WPF 精修篇 数据绑定到对象

Posted lonelyxmas

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了WPF 精修篇 数据绑定到对象相关的知识,希望对你有一定的参考价值。

原文: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. <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>

前台WPF绑定 

这里也可以用 INotifyPropertyChanged 也可以

 

集合使用ObservableCollenction<对象>

以上是关于WPF 精修篇 数据绑定到对象的主要内容,如果未能解决你的问题,请参考以下文章

WPF 精修篇 数据触发器

WPF 精修篇 用户控件

WPF 精修篇 依赖属性

WPF 精修篇 路径动画

WPF 精修篇 DataGrid 数据源排序

WPF 精修篇 全局为处理异常处理