博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
WPF的binding
阅读量:4303 次
发布时间:2019-05-27

本文共 2547 字,大约阅读时间需要 8 分钟。

  在WPF中Binding可以比作数据的桥梁,桥梁的两端分别是Binding的源(Source)和目标(Target)。一般情况下,Binding源是逻辑层对象,Binding目标是UI层的控件对象;这样,数据就会通过Binding送达UI层,被UI层展现。

   首先我们创建一个名为Student的类,这个类的实例作为数据源在UI上显示:  

public class Student    {        private string name;        public string Name        {            set { name = value; }            get { return name; }        }    }

  Binding是一种自动机制,当值变化后属性要有能力通知Binding,让Binding把变化传递给UI元素。怎样才能让一个属性具备这种通知Binding值已经变化的能力呢?方法是在属性的set语句中激发一个PropertyChanged事件。这个事件不需要我们自己声明,我们要做的是让作为数据源的类实现System.ComponentModel名称空间中的INotifyPropertyChanged接口。当为Binding设置了数据源后,Binding就会自动侦听来自这个接口的PropertyChanged事件。  

public class Student:INotifyPropertyChanged    {        private string name;        public string Name        {            set             {                 name = value;                NotifyPropertyChanged("Name");            }            get { return name; }        }        public event PropertyChangedEventHandler PropertyChanged;        private void NotifyPropertyChanged(string propertyName)        {            if (PropertyChanged != null)            {                PropertyChanged(this,new PropertyChangedEventArgs(propertyName));            }        }    }

  当Name属性的值发生变化时PropertyChanged事件就会被激发,Binding接收到这个事件后发现事件的消息告诉它是名为Name的属性发生了值得改变,于是就会通知Binding目标端的UI元素显示新的值。

  XAML代码:  

  C#代码:  

public partial class MainWindow : Window    {        private Student student;        public MainWindow()        {            InitializeComponent();            student = new Student();            Binding binding = new Binding();            binding.Source = student;            binding.Path = new PropertyPath("Name");            BindingOperations.SetBinding(this.Box,TextBox.TextProperty,binding);        }        private void Button_Click(object sender, RoutedEventArgs e)        {            student.Name = DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss");        }    }

  其中:Binding binding=new Binding()声明Binding类型变量并创建实例,然后使用binding.Source=student为Binding实例指定数据源,最后使用binding.Path = new PropertyPath("Name")为Binding指定访问路径。把数据源和目标连接在一起的任务是使用BindingOperations.SetBinding(this.Box,TextBox.TextProperty,binding)方法完成的。

  BindingOperations.SetBinding(this.Box,TextBox.TextProperty,binding)中的参数介绍:

  第一个参数主要是指定Binding的目标。

  第二个参数是用于为Binding指明将数据绑定到目标的那个属性中去,一般都为目标的依赖属性。

  第三个参数是指定那个Binding将数据源与目标关联起来。

  上面的代码还可以简化如下:

private void SetBinding()        {            student = new Student();            this.Box.SetBinding(TextBox.TextProperty, new Binding("Name") { Source=student});        }

 

  Binding模型如下:

 

转载地址:http://zslws.baihongyu.com/

你可能感兴趣的文章
Quartz-集群
查看>>
Eclipse-习惯设置
查看>>
Eclipse-Mybatis-generatorConfig.xml
查看>>
SpringBoot-Feign
查看>>
Eclipse-maven项目不将resources下的文件打到classpath下
查看>>
Linux-Centos安装JDK
查看>>
Maven-依赖调解/查看项目依赖了哪些包
查看>>
Maven-dependencyManagement与import
查看>>
SpringBoot-WebMvcAutoConfiguration/WebMvcConfigurer/WebMvcRegistrationsAdapter
查看>>
SpringMvc-ResponseBodyAdvice接口与@ControllerAdvice注解
查看>>
Spring-tx-PlatformTransactionManager(DataSourceTransactionManager)
查看>>
Spring-BeanDefinitionRegistry
查看>>
Spring-ClassPathBeanDefinitionScanner
查看>>
复制导致两台虚拟机ping不通
查看>>
打开chm文件,不显示文件内容的解决办法
查看>>
jvm基础知识一
查看>>
jvm基础知识二
查看>>
jvm基础知识三-----基本垃圾回收算法
查看>>
jvm基础知识四---垃圾回收面临的问题
查看>>
jvm基础知识五----分代垃圾回收详细
查看>>