兴义市住房和城乡建设局网签网站,武昌手机网站,wordpress多说读者墙,做网站页面一般设置多大尺寸先看效果#xff1a; 上面的绑定值都是我们自定义的属性#xff0c;有了以上的提示#xff0c;那么我们可以轻松绑定字段#xff0c;再也不用担心错误了。附带源码。 目录
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 ObservableCollectionStudentInfo Infos { get; set; }public MainWindowViewModel(){Infos new ObservableCollectionStudentInfo(){new StudentInfo(){ Id1, Age11, NameTom,Bfalse},new StudentInfo(){ Id2, Age12, NameDarren,Bfalse},new StudentInfo(){ Id3, Age13, NameJacky,Bfalse},new StudentInfo(){ Id4, Age14, NameAndy,Bfalse}};}}
}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/// /summarypublic partial class MainWindow : Window{public MainWindow(){InitializeComponent();this.DataContext new MainWindowViewModel();}}
}2.其中MainWindow.xaml
Window x:ClassWpfDataContextDemo.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfDataContextDemomc:IgnorabledTitleMainWindow Height450 Width800GridStackPanel Height295 HorizontalAlignmentLeft Margin10,10,0,0 NamestackPanel1 VerticalAlignmentTop Width427TextBlock Text{Binding Infos.Count } Margin5 /TextBlock Height23 NametextBlock1 Text学员编号: /TextBox Height23 NametxtStudentId Width301 HorizontalAlignmentLeft /TextBlock Height23 NametextBlock2 Text学员列表: /ListBox Height156 NamelbStudent Width305 HorizontalAlignmentLeft ItemsSource{Binding Infos}ListBox.ItemTemplateDataTemplateStackPanel NamestackPanel2 OrientationHorizontalTextBlock Text{Binding Id ,ModeTwoWay} Margin5 BackgroundRed/TextBlock x:Nameaa Text{Binding Name,ModeTwoWay, UpdateSourceTriggerExplicit} Margin5/TextBlock Text{Binding Age,ModeTwoWay} Margin5/TextBlock Text{Binding B,ModeTwoWay} Margin5//StackPanel/DataTemplate/ListBox.ItemTemplate/ListBox/StackPanel/Grid
/Window此时我们输入Binding的时候不会显示IdName这些字段。
3.xaml前台使用DataContext绑定
1.先屏蔽后台cs的代码 2.前台MainWindow.xaml
Window x:ClassWpfDataContextDemo.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfDataContextDemo xmlns:local1clr-namespace:WpfDataContextDemo.ViewModelmc:IgnorabledTitleMainWindow Height450 Width800Window.DataContextlocal1:MainWindowViewModel //Window.DataContextGridStackPanel Height295 HorizontalAlignmentLeft Margin10,10,0,0 NamestackPanel1 VerticalAlignmentTop Width427TextBlock Text{Binding Infos.Count } Margin5 /TextBlock Height23 NametextBlock1 Text学员编号: /TextBox Height23 NametxtStudentId Width301 HorizontalAlignmentLeft /TextBlock Height23 NametextBlock2 Text学员列表: /ListBox Height156 NamelbStudent Width305 HorizontalAlignmentLeft ItemsSource{Binding in}ListBox.ItemTemplateDataTemplateStackPanel NamestackPanel2 OrientationHorizontalTextBlock Text{Binding Id ,ModeTwoWay} Margin5 BackgroundRed/TextBlock x:Nameaa Text{Binding Name,ModeTwoWay, UpdateSourceTriggerExplicit} Margin5/TextBlock Text{Binding Age,ModeTwoWay} Margin5/TextBlock Text{Binding B,ModeTwoWay} Margin5//StackPanel/DataTemplate/ListBox.ItemTemplate/ListBox/StackPanel/Grid
/Window3.最主要的是可以点击出来非常的清晰明了 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 ObservableCollectionStudentInfo Infos { get; set; }//public MainWindowViewModel()//{// Infos new ObservableCollectionStudentInfo()// {// new StudentInfo(){ Id1, Age11, NameTom,Bfalse},// new StudentInfo(){ Id2, Age12, NameDarren,Bfalse},// new StudentInfo(){ Id3, Age13, NameJacky,Bfalse},// new StudentInfo(){ Id4, Age14, NameAndy,Bfalse}// };//}private static readonly MainWindowViewModel instance new MainWindowViewModel();public static MainWindowViewModel Instance{get { return instance; }}public ObservableCollectionStudentInfo Infos { get; set; }public MainWindowViewModel(){Infos new ObservableCollectionStudentInfo(){new StudentInfo(){ Id1, Age11, NameTom,Bfalse},new StudentInfo(){ Id2, Age12, NameDarren,Bfalse},new StudentInfo(){ Id3, Age13, NameJacky,Bfalse},new StudentInfo(){ Id4, Age14, NameAndy,Bfalse}};}}
}2.MainWindow.xaml
Window x:ClassWpfDataContextDemo.MainWindowxmlnshttp://schemas.microsoft.com/winfx/2006/xaml/presentationxmlns:xhttp://schemas.microsoft.com/winfx/2006/xamlxmlns:dhttp://schemas.microsoft.com/expression/blend/2008xmlns:mchttp://schemas.openxmlformats.org/markup-compatibility/2006xmlns:localclr-namespace:WpfDataContextDemo xmlns:local1clr-namespace:WpfDataContextDemo.ViewModelmc:IgnorabledDataContext{x:Static local1:MainWindowViewModel.Instance}TitleMainWindow Height450 Width800!--Window.DataContextlocal1:MainWindowViewModel //Window.DataContext--GridStackPanel Height295 HorizontalAlignmentLeft Margin10,10,0,0 NamestackPanel1 VerticalAlignmentTop Width427TextBlock Text{Binding Infos.Count } Margin5 /TextBlock Height23 NametextBlock1 Text学员编号: /TextBox Height23 NametxtStudentId Width301 HorizontalAlignmentLeft /TextBlock Height23 NametextBlock2 Text学员列表: /ListBox Height156 NamelbStudent Width305 HorizontalAlignmentLeft ItemsSource{Binding Infos}ListBox.ItemTemplateDataTemplateStackPanel NamestackPanel2 OrientationHorizontalTextBlock Text{Binding Id ,ModeTwoWay} Margin5 BackgroundRed/TextBlock x:Nameaa Text{Binding Name ,ModeTwoWay, UpdateSourceTriggerExplicit} Margin5/TextBlock Text{Binding Age,ModeTwoWay} Margin5/TextBlock Text{Binding B,ModeTwoWay} Margin5//StackPanel/DataTemplate/ListBox.ItemTemplate/ListBox/StackPanel/Grid
/Window源码https://download.csdn.net/download/u012563853/88407490 来源WPF中DataContext的绑定技巧_故里2130的博客-CSDN博客