WPF 精修篇 数据绑定到对象

2021-01-23 06:15

阅读:828

标签:ext   article   ring   store   anim   pre   ado   blog   static   

原文:WPF 精修篇 数据绑定到对象

 技术图片

数据绑定到对象 

首先 我们需要一个对象

  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. }

设置依赖属性绑定数据上下文

  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. "Right" Width="517">
  2. "109*"/>
  3. "112*"/>
  4. "94*"/>
  5. "202*"/>
  6. "24*"/>
  7. "25*"/>
  8. "25*"/>
  9. "86*"/>
  10. "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" />
  11. "Left" Margin="41,22,0,0" TextWrapping="Wrap" Text="名字" VerticalAlignment="Top"/>
  12. "Left" Margin="79,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Name}" Width="30"/>
  13. "Left" Margin="10,22,0,0" TextWrapping="Wrap" Text="address" VerticalAlignment="Top" Grid.Column="1"/>
  14. "63.667,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Address}" Grid.Column="1"/>
  15. "Left" Margin="20,22,0,0" TextWrapping="Wrap" Text="age" VerticalAlignment="Top" Grid.Column="2"/>
  16. "57,22,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Text="{Binding Age}" Grid.Column="2"/>
  17. "Left" Height="23" Margin="0,17,0,0" Grid.Row="1" TextWrapping="Wrap" VerticalAlignment="Top" Width="120" Grid.ColumnSpan="2" Grid.Column="2" />

前台WPF绑定 

这里也可以用 INotifyPropertyChanged 也可以

 

集合使用ObservableCollenction

WPF 精修篇 数据绑定到对象

标签:ext   article   ring   store   anim   pre   ado   blog   static   

原文地址:https://www.cnblogs.com/lonelyxmas/p/12075447.html


评论


亲,登录后才可以留言!