WPF第一关Bingding怎么用



这两天在学MVVM:Model-View-ViewModel。就是一种写程序的的框架,目的是将程序按照三种功能分类,在project上新建三个文件夹加(Models,Views,ViewModels),然后大家写的对应功能程序,分别放到对应的文件夹内。这样有几个好处,

一、当用户界面改变时,只要界面对应的程序就行了,不用改其他部分的程序。

二、当写某一部分程序的工程师离职了,其他工程师能很快的看懂他写的程序等等。

最终的程序效果:遵循“开 闭”原则,“开”指对用户开放,就是界面用户觉得不满意,可以随便改,但底层代码要“闭”,就是闭合,不作改动,或只做很小的改动就能对应用户的需要。

为了实现这种功能,Bingding是第一关(个人观点)

比如下面的例子,把label1的内容和Name绑定,将button1和Zhangsan绑定,当点击按钮是,显示“张三”。这就是Bingding最简单的用法。程序没写注释,下次不这样了!!!!

下面上实例

一、新建WPF文件:CSDN-Bingding。开始时XAML的样子如下

<Window x:Class="CSDN_Bingding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
       
    </Grid>
</Window>

二、XAML改成这样

<Window x:Class="CSDN_Bingding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <StackPanel>
        <Label Name="label1" Content="{Binding Path=Name}" Background="AliceBlue" Width="200" Height="50"/>
        <Button Name="button1" Command="{Binding Zhangsan}" Width="100" Height="50" />
    </StackPanel> 
</Window>

三、新建一个叫MyCommand的类(右击Project,再点add,你懂的),这个类继承Icommand接口,用于绑定命令,如button1。程序内容如下。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace CSDN_Bingding
{
    class MyCommand : ICommand  //注意要导入命名空间 using System.Windows.Input;
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

        public event EventHandler CanExecuteChanged;

        public void Execute(object parameter)
        {
            this.ExecuteAction(parameter);
        }
        public Action<object> ExecuteAction { get; set; }
    }
}

四、新建一个叫PropertyMonitor的类(右击Project,再点add,你懂的),这个类继承INotifyPropertyChanged接口,用于绑定数据用。程序内容如下

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CSDN_Bingding
{
    class PropertyMonitor:INotifyPropertyChanged //注意要导入命名空间 using System.ComponentModel;
    {
            private string _name;
        public string Name
        {
            get { return _name; }
            set
            {
                _name = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Name"));
                }
            }
        }
        private void ZhangsanData(object parameter)
        {
            this.Name = "张三";
        }
        public event PropertyChangedEventHandler PropertyChanged;
        public MyCommand Zhangsan { get; set; }
        public PropertyMonitor()
        {
            this.Zhangsan = new MyCommand();
            this.Zhangsan.ExecuteAction = new Action<object>(this.ZhangsanData);
        }

    }
}

五、最后将MainWindow.xaml.cs的构造函数加一句this.DataContext = new PropertyMonitor();

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;

namespace CSDN_Bingding
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            this.DataContext = new PropertyMonitor();//只多写这一步
        }
    }
}

最终效果:点击按钮,label显示张三。大功告成!!

猜你喜欢

转载自blog.csdn.net/sklzl1571/article/details/79795711