WPF中使用MVVM模型进行数据绑定


前言

WPF数据绑定对于WPF应用程序来说尤为重要,本文将讲述使用MVVM模式进行数据绑定的四步走用法:
我们可以先在项目下新建几个目录:Core、Models、ViewModels
在这里插入图片描述


一、声明一个类用来实现接口 INotifyPropertyChanged

在Core文件夹新建类文件:NotifyPropertyObject .cs
示例代码如下

public class NotifyPropertyObject : INotifyPropertyChanged
{
    
    
    public event PropertyChangedEventHandler PropertyChanged;

    public void OnPropertyChanged(string propertyName)
    {
    
    
        if(PropertyChanged != null)
        {
    
    
            PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }


}

二、实例化ViewModel对象

1.新建MainViewModel模型类

在ViewModels文件夹新建类文件:MainViewModel .cs
示例代码如下

public class MainViewModel: NotifyPropertyObject
{
    
    
     private int _MyValue;

     public int MyValue
     {
    
    
         get {
    
     return _MyValue; }
         set
         {
    
     
             _MyValue = value;
             OnPropertyChanged("MyValue");
         }
     }

     public List<string> StrList {
    
     get; set; }

     public MyStudent MyStudent {
    
     get; set; }

     public MainViewModel()
     {
    
    
         MyStudent = new MyStudent();

         StrList = new List<string>() {
    
     "1","2","3","4","5"};

     }
 }

2.实例化对象

在App.xaml.cs中编辑
代码如下(示例):

public static ViewModels.MainViewModel MainViewModel {
    
     get; private set; }

public App()
{
    
    
    MainViewModel = new ViewModels.MainViewModel();
}

在主界面后台代码中声明ViewModel

public partial class MainWindow : Window
{
    
    
    public ViewModels.MainViewModel ViewModel {
    
     get {
    
     return App.MainViewModel; } }
    
	public MainWindow()
    {
    
    
        InitializeComponent();

        this.DataContext = ViewModel;

        this.Loaded += MainWindow_Loaded;
    }
}

为每一个界面建立其模型类,如MainViewModel,AzimuthWindowVM…,此种类型的类同样需继承自类NotifyPropertyObject
注意:(1)此类中主要添加后台的实现代码,其一是使后台代码尽可能简单;其二是便于数据的绑定(2)此类中大多数情况下写的是属性和方法


三、在界面设计代码中进行绑定

.xaml文件

<StackPanel Orientation="Vertical" Grid.Row="0">
    <TextBox Text="{Binding MyStudent.Age ,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Width="100" Height="40" Margin="10"></TextBox>
    <Button Content="Click1" Click="Button_Click1" Width="100" Height="50"></Button>
</StackPanel>

<StackPanel Orientation="Vertical" Grid.Row="1">
    <TextBox Text="{Binding MyValue ,Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Width="100" Height="40" Margin="10"></TextBox>
    <Button Content="Click" Click="Button_Click" Width="100" Height="50"></Button>
</StackPanel>

四、应用

后台代码:

 private void Button_Click(object sender, RoutedEventArgs e)
 {
    
    
     Console.WriteLine(ViewModel.MyValue);

     ViewModel.MyValue = 20;

     //OneWayToSource ----编辑框编辑值时 ViewModel.MyValue可以获取到值,但是ViewModel.MyValue改变时,binding的编辑框无法更改
     //OneWay         ----编辑框编辑值时 ViewModel.MyValue值无法更新。但是ViewModel.MyValue改变时,binding的编辑框更新了
     //TwoWay         ----编辑框编辑值时 ViewModel.MyValue可以获取到值,且ViewModel.MyValue改变时,binding的编辑框也更新了

     //对于OneWay绑定:在界面中显示的数据可以随数据源的值的变化而变化,但更改界面的数据不会影响到数据源。
     //对于TwoWay绑定:界面中显示的数据及数据源的数据可以双向显示及更新。
     //对于OneWayToSource绑定:初始时界面的数据为空;更改界面的数据可以影响数据源的值,但是更改数据源的值不会体现在界面上。
     //对于OneTime绑定:在界面中显示的为数据源的初始值,更改数据源的值的时候,不会更改界面的数据显示;更改界面的数据也不会影响到数据源的数据。


     //UpdateSourceTrigger 属性的角色:
     //PropertyChanged:当绑定目标属性更改时,立即更新绑定源。
     //LostFocus:当绑定目标元素失去焦点时,更新绑定源。
     //Explicit:仅在调用 UpdateSource 方法时更新绑定源。
     //注释:多数依赖项属性的UpdateSourceTrigger 值的默认值为 PropertyChanged,而 Text 属性的默认值为 LostFocus。
 }

 private void Button_Click1(object sender, RoutedEventArgs e)
 {
    
    
     Console.WriteLine(ViewModel.MyStudent.Age);
 }

猜你喜欢

转载自blog.csdn.net/BeanGo/article/details/128036541
今日推荐