WPF data binding articles refinement Update Notification

Original: WPF data binding finishing papers Update Notifications

Interestingly, the point to start the update

First Data Binding One of the Element Binding

See examples


  
  
  1. <Window x:Class= "WpfApplication20.MainWindow"
  2. xmlns= "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3. xmlns:x= "http://schemas.microsoft.com/winfx/2006/xaml"
  4. Title= "MainWindow" Height= "156.383" Width= "246.489" x:Name= "MWindow">
  5. <Grid>
  6. <TextBox HorizontalAlignment= "Left" Height= "23" Margin= "30,48,0,0" TextWrapping= "Wrap" VerticalAlignment= "Top" Width= "120" Text= "{Binding ElementName=MWindow,Path=PresonName}"/>
  7. <TextBlock HorizontalAlignment= "Left" Margin= "30,16,0,0" TextWrapping= "Wrap" VerticalAlignment= "Top" Width= "42" Text= "部门:"/>
  8. <Button Content= "提交" HorizontalAlignment= "Left" Margin= "30,85,0,0" VerticalAlignment= "Top" Width= "75" Click= "Button_Click"/>
  9. <Button Content= "重置" HorizontalAlignment= "Left" Margin= "127,85,0,0" VerticalAlignment= "Top" Width= "75" Click= "Button_Click_1"/>
  10. <TextBlock HorizontalAlignment= "Left" Margin= "85,16,0,0" TextWrapping= "Wrap" VerticalAlignment= "Top" Text= "{Binding ElementName=MWindow,Path=Department}"/>
  11. </Grid>
  12. </Window>

  
  
  1. public partial class MainWindow : Window, INotifyPropertyChanged
  2. {
  3. public MainWindow()
  4. {
  5. InitializeComponent();
  6. }
  7. private string department;
  8. public string Department
  9. {
  10. get { return "软件开发"; }
  11. }
  12. private string presonName;
  13. public string PresonName
  14. {
  15. get { return presonName; }
  16. set { presonName = value;
  17. OnPropertyChanged( "PresonName");
  18. }
  19. }
  20. private void Button_Click(object sender, RoutedEventArgs e)
  21. {
  22. MessageBox.Show( "Hi," + PresonName);
  23. }
  24. private void Button_Click_1(object sender, RoutedEventArgs e)
  25. {
  26. PresonName = "";
  27. }
  28. #region INotifyPropertyChanged 成员
  29. public event PropertyChangedEventHandler PropertyChanged;
  30. public void OnPropertyChanged(string name)
  31. {
  32. if(PropertyChanged!= null){
  33. PropertyChanged.Invoke( this, new PropertyChangedEventArgs(name));
  34. }
  35. }
  36. #endregion
  37. }

We have two operations in this example

A data binding

A data notification

Element Binding is specified window because the window after window to create a form has this attribute by attribute name. How properties

Notification needs to inherit INotifyPropertyChanged

Implement an interface method

  public event PropertyChangedEventHandler PropertyChanged;

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

In the properties need to be informed of the following plus

OnPropertyChanged ( "" property name "");

They can play the role of the notification

Guess you like

Origin www.cnblogs.com/lonelyxmas/p/12075441.html