专注收集记录技术开发学习笔记、技术难点、解决方案
网站信息搜索 >> 请输入关键词:
您当前的位置: 首页 > WPF

传智播客-数据绑定-INotifyPropertyChanged(小白内容)

发布时间:2011-06-23 13:56:34 文章来源:www.iduyao.cn 采编人员:星星草
传智播客--数据绑定--INotifyPropertyChanged(小白内容)

INotifyPropertyChanged一般在数据绑定的时候使用。

InotifyPropertyChanged是.net内置的接口,数据绑定时会检测DataContext是否实现了InotifyPropertyChanged,如果实现了,就会监听PropertyChanged,得知属性变化。

可以理解为InotifyPropertyChanged接口用于向客户端发出某一属性值已更改的通知。

class Person:INotifyPropertyChanged
    {
        private int age;
        public int Age
        {
            get
            {
                return age;
            }

            set
            {
                this.age = value;
                if (PropertyChanged != null)
                {
                    PropertyChanged(this, new PropertyChangedEventArgs("Age"));
                }
            }
        }
        public event PropertyChangedEventHandler PropertyChanged;
    }

.cs文件

 public partial class MainWindow : Window
    {
        private Person p = new Person();
        public MainWindow()
        {
            InitializeComponent();           

        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            p.Age++;
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            p.Age = 10;
            txtAge.DataContext = p;
        }
    }

xaml文件

<TextBox Name="txtAge" HorizontalAlignment="Left" Height="23" Margin="82,39,0,0" TextWrapping="Wrap" Text="{Binding Age}" VerticalAlignment="Top" Width="120"/>
<Button Content="Age++" HorizontalAlignment="Left" Margin="271,39,0,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>

这样点击button可以实现textbox里的数字自增。

 

友情提示:
信息收集于互联网,如果您发现错误或造成侵权,请及时通知本站更正或删除,具体联系方式见页面底部联系我们,谢谢。

其他相似内容:

热门推荐: