Chuan Zhi podcast -- Data Binding -- INotifyPropertyChanged (), Chuan Zhi podcast

Source: Internet
Author: User

Chuan Zhi podcast -- Data Binding -- INotifyPropertyChanged (), Chuan Zhi podcast

INotifyPropertyChanged is generally used for data binding.

InotifyPropertyChanged is a built-in. net interface. When binding data, it checks whether DataContext has implemented InotifyPropertyChanged. If so, it listens to PropertyChanged and learns that the property has changed.

The InotifyPropertyChanged interface is used to send a notification that a property value has been changed to the client.

Class

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 File

 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 files

<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"/>

In this way, you can click the button to realize the auto-increment of numbers in the textbox.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.