先看效果:
上面的绑定值都是我们自定义的属性,有了以上的提示,那么我们可以轻松绑定字段,再也不用担心错误了。附带源码。
目录
1.建立mvvm项目
2.cs后台使用DataContext绑定
3.xaml前台使用DataContext绑定
4.xaml前台使用DataContext单例模式绑定
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 Infos { get; set; }
public MainWindowViewModel()
{
Infos = new ObservableCollection()
{
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}
};
}
}
}
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
{
///
/// Interaction logic for MainWindow.xaml
///
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
this.DataContext = new MainWindowViewModel();
}
}
}
2.其中MainWindow.xaml
此时,我们输入Binding的时候,不会显示Id,Name这些字段。
1.先屏蔽后台cs的代码
2.前台MainWindow.xaml
3.最主要的是,可以点击出来,非常的清晰明了
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 Infos { get; set; }
//public MainWindowViewModel()
//{
// Infos = new ObservableCollection()
// {
// 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 Infos { get; set; }
public MainWindowViewModel()
{
Infos = new ObservableCollection()
{
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
源码:https://download.csdn.net/download/u012563853/88407490
来源:WPF中DataContext的绑定技巧_故里2130的博客-CSDN博客