当前位置: 首页 > news >正文

李鸿星电子商务网站建设网站索引查询

李鸿星电子商务网站建设,网站索引查询,网站内外链建设,二次开发什么意思先看效果: 上面的绑定值都是我们自定义的属性,有了以上的提示,那么我们可以轻松绑定字段,再也不用担心错误了。附带源码。 目录 1.建立mvvm项目 2.cs后台使用DataContext绑定 3.xaml前台使用DataContext绑定 4.xaml前台使用Da…

先看效果:

上面的绑定值都是我们自定义的属性,有了以上的提示,那么我们可以轻松绑定字段,再也不用担心错误了。附带源码。

目录

1.建立mvvm项目

2.cs后台使用DataContext绑定

3.xaml前台使用DataContext绑定

4.xaml前台使用DataContext单例模式绑定


1.建立mvvm项目

1.首先建立一个项目,采用mvvm模式,其中MainWindow.xaml相当于View层,其余各层都比较简单。

2.BindingBase.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;namespace WpfDataContextDemo.Common
{public class BindingBase : INotifyPropertyChanged{public event PropertyChangedEventHandler PropertyChanged;//protected virtual void OnPropertyChanged(string propertyName)protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = "")//此处使用特性{PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));}}
}

3.StudentInfo.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfDataContextDemo.Common;namespace WpfDataContextDemo.Model
{public class StudentInfo : BindingBase{private int id;public int Id{get { return id; }set { id = value; OnPropertyChanged(); }}private string name;public string Name{get { return name; }set { name = value; OnPropertyChanged(); }}private int age;public int Age{get { return age; }set { age = value; OnPropertyChanged(); }}private bool b;public bool B{get { return b; }set { b = value; OnPropertyChanged(); }}}
}

4.MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfDataContextDemo.Model;namespace WpfDataContextDemo.ViewModel
{public class MainWindowViewModel{public ObservableCollection<StudentInfo> Infos { get; set; }public MainWindowViewModel(){Infos = new ObservableCollection<StudentInfo>(){new StudentInfo(){ Id=1, Age=11, Name="Tom",B=false},new StudentInfo(){ Id=2, Age=12, Name="Darren",B=false},new StudentInfo(){ Id=3, Age=13, Name="Jacky",B=false},new StudentInfo(){ Id=4, Age=14, Name="Andy",B=false}};}}
}

2.cs后台使用DataContext绑定

1.MainWindow.xaml.cs

只有一句话最重要

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfDataContextDemo.ViewModel;namespace WpfDataContextDemo
{/// <summary>/// Interaction logic for MainWindow.xaml/// </summary>public partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext = new MainWindowViewModel();}}
}

2.其中MainWindow.xaml

<Window x:Class="WpfDataContextDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfDataContextDemo"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel Height="295" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="427"><TextBlock  Text="{Binding Infos.Count  }" Margin="5" /><TextBlock Height="23" Name="textBlock1" Text="学员编号:" /><TextBox Height="23" Name="txtStudentId"  Width="301" HorizontalAlignment="Left"   /><TextBlock Height="23" Name="textBlock2" Text="学员列表:" /><ListBox Height="156" Name="lbStudent" Width="305" HorizontalAlignment="Left" ItemsSource="{Binding   Infos}"><ListBox.ItemTemplate><DataTemplate><StackPanel Name="stackPanel2" Orientation="Horizontal"><TextBlock  Text="{Binding Id  ,Mode=TwoWay}" Margin="5" Background="Red"/><TextBlock x:Name="aa" Text="{Binding Name,Mode=TwoWay, UpdateSourceTrigger=Explicit}" Margin="5"/><TextBlock  Text="{Binding  Age,Mode=TwoWay}" Margin="5"/><TextBlock  Text="{Binding B,Mode=TwoWay}" Margin="5"/></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox></StackPanel></Grid>
</Window>

此时,我们输入Binding的时候,不会显示Id,Name这些字段。

3.xaml前台使用DataContext绑定

1.先屏蔽后台cs的代码

2.前台MainWindow.xaml 

<Window x:Class="WpfDataContextDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfDataContextDemo" xmlns:local1="clr-namespace:WpfDataContextDemo.ViewModel"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Window.DataContext><local1:MainWindowViewModel /></Window.DataContext><Grid><StackPanel Height="295" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="427"><TextBlock  Text="{Binding Infos.Count  }" Margin="5" /><TextBlock Height="23" Name="textBlock1" Text="学员编号:" /><TextBox Height="23" Name="txtStudentId"  Width="301" HorizontalAlignment="Left"   /><TextBlock Height="23" Name="textBlock2" Text="学员列表:" /><ListBox Height="156" Name="lbStudent" Width="305" HorizontalAlignment="Left" ItemsSource="{Binding  in}"><ListBox.ItemTemplate><DataTemplate><StackPanel Name="stackPanel2" Orientation="Horizontal"><TextBlock  Text="{Binding Id  ,Mode=TwoWay}" Margin="5" Background="Red"/><TextBlock x:Name="aa" Text="{Binding Name,Mode=TwoWay, UpdateSourceTrigger=Explicit}" Margin="5"/><TextBlock  Text="{Binding  Age,Mode=TwoWay}" Margin="5"/><TextBlock  Text="{Binding B,Mode=TwoWay}" Margin="5"/></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox></StackPanel></Grid>
</Window>

3.最主要的是,可以点击出来,非常的清晰明了

4.xaml前台使用DataContext单例模式绑定

1.MainWindowViewModel.cs

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WpfDataContextDemo.Model;namespace WpfDataContextDemo.ViewModel
{public class MainWindowViewModel{//public ObservableCollection<StudentInfo> Infos { get; set; }//public MainWindowViewModel()//{//    Infos = new ObservableCollection<StudentInfo>()//    {//        new StudentInfo(){ Id=1, Age=11, Name="Tom",B=false},//        new StudentInfo(){ Id=2, Age=12, Name="Darren",B=false},//        new StudentInfo(){ Id=3, Age=13, Name="Jacky",B=false},//        new StudentInfo(){ Id=4, Age=14, Name="Andy",B=false}//    };//}private static readonly MainWindowViewModel instance = new MainWindowViewModel();public static MainWindowViewModel Instance{get { return instance; }}public ObservableCollection<StudentInfo> Infos { get; set; }public MainWindowViewModel(){Infos = new ObservableCollection<StudentInfo>(){new StudentInfo(){ Id=1, Age=11, Name="Tom",B=false},new StudentInfo(){ Id=2, Age=12, Name="Darren",B=false},new StudentInfo(){ Id=3, Age=13, Name="Jacky",B=false},new StudentInfo(){ Id=4, Age=14, Name="Andy",B=false}};}}
}

2.MainWindow.xaml

<Window x:Class="WpfDataContextDemo.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft.com/expression/blend/2008"xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"xmlns:local="clr-namespace:WpfDataContextDemo" xmlns:local1="clr-namespace:WpfDataContextDemo.ViewModel"mc:Ignorable="d"DataContext="{x:Static local1:MainWindowViewModel.Instance}"Title="MainWindow" Height="450" Width="800"><!--<Window.DataContext><local1:MainWindowViewModel /></Window.DataContext>--><Grid><StackPanel Height="295" HorizontalAlignment="Left" Margin="10,10,0,0" Name="stackPanel1" VerticalAlignment="Top" Width="427"><TextBlock  Text="{Binding Infos.Count  }" Margin="5" /><TextBlock Height="23" Name="textBlock1" Text="学员编号:" /><TextBox Height="23" Name="txtStudentId"  Width="301" HorizontalAlignment="Left"   /><TextBlock Height="23" Name="textBlock2" Text="学员列表:" /><ListBox Height="156" Name="lbStudent" Width="305" HorizontalAlignment="Left" ItemsSource="{Binding  Infos}"><ListBox.ItemTemplate><DataTemplate><StackPanel Name="stackPanel2" Orientation="Horizontal"><TextBlock  Text="{Binding Id  ,Mode=TwoWay}" Margin="5" Background="Red"/><TextBlock x:Name="aa" Text="{Binding Name ,Mode=TwoWay, UpdateSourceTrigger=Explicit}" Margin="5"/><TextBlock  Text="{Binding  Age,Mode=TwoWay}" Margin="5"/><TextBlock  Text="{Binding B,Mode=TwoWay}" Margin="5"/></StackPanel></DataTemplate></ListBox.ItemTemplate></ListBox></StackPanel></Grid>
</Window>

 源码:https://download.csdn.net/download/u012563853/88407490

来源:WPF中DataContext的绑定技巧_故里2130的博客-CSDN博客

http://www.yayakq.cn/news/31497/

相关文章:

  • 企业公司网站制作电子商务以后可以做什么工作
  • 烘焙甜点培训学校站长工具seo综合查询访问
  • 汕头市国外网站建设公司凡客集团
  • 义乌 外贸网站 开发集团网站推广
  • 佛山网站建设设计公司深圳外贸网站建设服务收费
  • 南昌企业网站设计建设制作营销网站建设哪个平台好
  • 网页设计与网站建设实训报告wordpress升级需要ftp
  • 雁塔区网站建设微信公众平台怎么注册
  • 杭州哪里可以做网站推广0317网站建设
  • 东南融通网站建设用五百丁做名字的简历网站
  • 怎样做打赏网站制作一个公司网站多少
  • 毕业设计做网站简单吗医疗网站几个人做竞价
  • 建设商城网站报价网络营销工程师有用吗
  • 药企网站怎么做天猫商城网上购物
  • 可以用自己的电脑做网站主机手机网站建设报价表
  • 怎么查看网站是否降权一个公司的管理流程
  • 帮忙做任务网站做淘宝的货源网站
  • 网站备案管局审核北京网站制作开发公司
  • h5网站搭建动力风网站建设及软件开发合同
  • 网络公司网站设计维护合同辽宁建设工程信息网怎么办锁
  • 网站开发 质量管理名牌网站设计的图片
  • 一个域名建多个网站wordpress 加广告位
  • wordpress改网站地址一个刚有官网的公司怎么做网站运营
  • 中小微企业查询网站成都关键词优化技术
  • 顺德网站建设itshunde不用网络的游戏
  • 网站设计要求wordpress 3.5.2
  • 开源站群cms网站标题在哪里修改
  • 什么网站可以接单做设计网站建设费用分录
  • 做网站时 404网页如何指向一般网站开发的硬件要求
  • 自做购物网站多少钱网站开发提问