code_junkie

Two-way binding in WPF

Nov 12th, 2011
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. <Window x:Class="DataBinding.MyWindow" ...
  2. Title="MyWindow" Height="300" Width="300">
  3. <StackPanel x:Name="TopLevelContainer">
  4. <TextBox x:Name="txtValue" Background="AliceBlue" Text="{Binding Path=MyDotNetProperty}" />
  5. <TextBlock TextWrapping="Wrap">We're twin blue boxes bound to the same property.</TextBlock>
  6. <TextBox x:Name="txtValue2" Background="AliceBlue" Text="{Binding Path=MyDotNetProperty}" />
  7. </StackPanel>
  8. </Window>
  9.  
  10. public partial class MyWindow : Window, INotifyPropertyChanged
  11. {
  12. public MyWindow()
  13. {
  14. InitializeComponent();
  15. this.MyDotNetProperty = "Go ahead. Change my value.";
  16. TopLevelContainer.DataContext = this;
  17. }
  18.  
  19. private string m_sValue;
  20. public string MyDotNetProperty
  21. {
  22. get { return m_sValue; }
  23. set
  24. {
  25. m_sValue = value;
  26. if (null != this.PropertyChanged)
  27. {
  28. PropertyChanged(this, new PropertyChangedEventArgs("MyDotNetProperty"));
  29. }
  30. }
  31. }
  32.  
  33. #region INotifyPropertyChanged Members
  34. public event PropertyChangedEventHandler PropertyChanged;
  35. #endregion
  36. }
Add Comment
Please, Sign In to add comment