WPF data binding finishing chapter to an object

Original: WPF data binding finishing chapter to an object

 

Data binding to target 

First, we need an object


  
  
  1. public class Preson
  2. {
  3. private string name;
  4. public string Name
  5. {
  6. get { return name; }
  7. set { name = value; }
  8. }
  9. private string address;
  10. public string Address
  11. {
  12. get { return address; }
  13. set { address = value; }
  14. }
  15. private int age;
  16. public int Age
  17. {
  18. get { return age; }
  19. set { age = value; }
  20. }
  21. }

Binding data context dependency attribute set


  
  
  1. public MainWindow()
  2. {
  3. InitializeComponent();
  4. Preson = new Preson() { Name = "慧哥", Address = "大连", Age = 30 };
  5. DataContext = Preson;
  6. }
  7. public Preson Preson
  8. {
  9. get { return (Preson)GetValue(PresonProperty); }
  10. set { SetValue(PresonProperty, value); }
  11. }
  12. // Using a DependencyProperty as the backing store for Preson. This enables animation, styling, binding, etc...
  13. public static readonly DependencyProperty PresonProperty =
  14. DependencyProperty.Register( "Preson", typeof(Preson), typeof(MainWindow), new PropertyMetadata( null));

 


  
  
  1. <Grid HorizontalAlignment= "Right" Width= "517">
  2. <Grid.ColumnDefinitions>
  3. <ColumnDefinition Width= "109*"/>
  4. <ColumnDefinition Width= "112*"/>
  5. <ColumnDefinition Width= "94*"/>
  6. <ColumnDefinition Width= "202*"/>
  7. </Grid.ColumnDefinitions>
  8. <Grid.RowDefinitions>
  9. <RowDefinition Height= "24*"/>
  10. <RowDefinition Height= "25*"/>
  11. <RowDefinition Height= "25*"/>
  12. <RowDefinition Height= "86*"/>
  13. </Grid.RowDefinitions>
  14. <TextBox HorizontalAlignment= "Left" Height= "23" Margin= "101,27,0,0" Grid.Row= "1" TextWrapping= "Wrap" Text= "{Binding Age,Mode=TwoWay}" VerticalAlignment= "Top" Width= "120" Grid.ColumnSpan= "2" />
  15. <TextBlock HorizontalAlignment= "Left" Margin= "41,22,0,0" TextWrapping= "Wrap" Text= "名字" VerticalAlignment= "Top"/>
  16. <TextBlock HorizontalAlignment= "Left" Margin= "79,22,0,0" TextWrapping= "Wrap" VerticalAlignment= "Top" Text= "{Binding Name}" Width= "30"/>
  17. <TextBlock HorizontalAlignment= "Left" Margin= "10,22,0,0" TextWrapping= "Wrap" Text= "address" VerticalAlignment= "Top" Grid.Column= "1"/>
  18. <TextBlock Margin= "63.667,22,0,0" TextWrapping= "Wrap" VerticalAlignment= "Top" Text= "{Binding Address}" Grid.Column= "1"/>
  19. <TextBlock HorizontalAlignment= "Left" Margin= "20,22,0,0" TextWrapping= "Wrap" Text= "age" VerticalAlignment= "Top" Grid.Column= "2"/>
  20. <TextBlock Margin= "57,22,0,0" TextWrapping= "Wrap" VerticalAlignment= "Top" Text= "{Binding Age}" Grid.Column= "2"/>
  21. <TextBox HorizontalAlignment= "Left" Height= "23" Margin= "0,17,0,0" Grid.Row= "1" TextWrapping= "Wrap" VerticalAlignment= "Top" Width= "120" Grid.ColumnSpan= "2" Grid.Column= "2" />
  22. </Grid>

Front Binding WPF 

Here you can also use INotifyPropertyChanged can

 

Use set ObservableCollenction <Object>

Guess you like

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