C# WPF入门学习主线篇(二十四)—— 数据绑定基础

王柳  金牌会员 | 2024-7-28 19:22:47 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 540|帖子 540|积分 1620

C# WPF入门学习主线篇(二十四)—— 数据绑定基础


数据绑定是WPF的紧张特性之一,它允许UI元素和数据源之间建立连接,从而实现数据的自动更新和显示。通过数据绑定,开发者可以减少大量的手动更新代码,使应用步调更具响应性和可维护性。本篇博客将详细介绍WPF数据绑定的基础知识,包罗单向绑定、双向绑定、绑定路径和数据上下文。
1. 数据绑定基础

数据绑定是指将控件的属性与数据源举行连接,使得控件的显示内容和数据源保持同步。WPF支持多种数据绑定模式,最常见的有以下几种:
1.1 单向绑定(OneWay)

单向绑定是指从数据源到目标控件的单向更新。控件会随着数据源的变化而自动更新,但控件的变化不会影响数据源。
示例:

  1. <Window x:Class="WpfApp.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="DataBinding Demo" Height="200" Width="300">
  5.     <Grid>
  6.         <TextBox Text="{Binding Name}" Width="200" Height="30" Margin="10"/>
  7.     </Grid>
  8. </Window>
复制代码
  1. using System.ComponentModel;
  2. using System.Windows;
  3. namespace WpfApp
  4. {
  5.     public partial class MainWindow : Window
  6.     {
  7.         public MainWindow()
  8.         {
  9.             InitializeComponent();
  10.             DataContext = new Person { Name = "Alice" };
  11.         }
  12.     }
  13.     public class Person : INotifyPropertyChanged
  14.     {
  15.         private string name;
  16.         public string Name
  17.         {
  18.             get { return name; }
  19.             set
  20.             {
  21.                 if (name != value)
  22.                 {
  23.                     name = value;
  24.                     OnPropertyChanged("Name");
  25.                 }
  26.             }
  27.         }
  28.         public event PropertyChangedEventHandler PropertyChanged;
  29.         protected void OnPropertyChanged(string propertyName)
  30.         {
  31.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  32.         }
  33.     }
  34. }
复制代码
在这个示例中,TextBox 的 Text 属性绑定到 Person 类的 Name 属性,当 Name 属性变化时,TextBox 会自动更新显示的内容。
1.2 双向绑定(TwoWay)

双向绑定允许数据源和目标控件之间的双向更新。控件的变化会自动反映到数据源,数据源的变化也会自动更新控件。
示例:

  1. <Window x:Class="WpfApp.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="DataBinding Demo" Height="200" Width="300">
  5.     <Grid>
  6.         <TextBox Text="{Binding Name, Mode=TwoWay}" Width="200" Height="30" Margin="10"/>
  7.         <Button Content="Update Name" Width="100" Height="30" Margin="10,50,0,0" Click="Button_Click"/>
  8.     </Grid>
  9. </Window>
复制代码
  1. using System.ComponentModel;
  2. using System.Windows;
  3. namespace WpfApp
  4. {
  5.     public partial class MainWindow : Window
  6.     {
  7.         public MainWindow()
  8.         {
  9.             InitializeComponent();
  10.             DataContext = new Person { Name = "Alice" };
  11.         }
  12.         private void Button_Click(object sender, RoutedEventArgs e)
  13.         {
  14.             (DataContext as Person).Name = "Bob";
  15.         }
  16.     }
  17.     public class Person : INotifyPropertyChanged
  18.     {
  19.         private string name;
  20.         public string Name
  21.         {
  22.             get { return name; }
  23.             set
  24.             {
  25.                 if (name != value)
  26.                 {
  27.                     name = value;
  28.                     OnPropertyChanged("Name");
  29.                 }
  30.             }
  31.         }
  32.         public event PropertyChangedEventHandler PropertyChanged;
  33.         protected void OnPropertyChanged(string propertyName)
  34.         {
  35.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  36.         }
  37.     }
  38. }
复制代码
在这个示例中,点击按钮会将 Person 类的 Name 属性更新为 “Bob”,同时 TextBox 中的文本也会自动更新。
2. 绑定路径和数据上下文

2.1 绑定路径

绑定路径用于指定绑定的数据源中的属性。可以利用点号(.)分隔多个属性,以便从嵌套对象中获取数据。
示例:

  1. <Window x:Class="WpfApp.MainWindow"
  2.         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  3.         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  4.         Title="DataBinding Demo" Height="200" Width="300">
  5.     <Grid>
  6.         <TextBox Text="{Binding Address.City}" Width="200" Height="30" Margin="10"/>
  7.     </Grid>
  8. </Window>
复制代码
  1. using System.ComponentModel;
  2. using System.Windows;
  3. namespace WpfApp
  4. {
  5.     public partial class MainWindow : Window
  6.     {
  7.         public MainWindow()
  8.         {
  9.             InitializeComponent();
  10.             DataContext = new Person
  11.             {
  12.                 Name = "Alice",
  13.                 Address = new Address { City = "New York" }
  14.             };
  15.         }
  16.     }
  17.     public class Person : INotifyPropertyChanged
  18.     {
  19.         private string name;
  20.         private Address address;
  21.         public string Name
  22.         {
  23.             get { return name; }
  24.             set
  25.             {
  26.                 if (name != value)
  27.                 {
  28.                     name = value;
  29.                     OnPropertyChanged("Name");
  30.                 }
  31.             }
  32.         }
  33.         public Address Address
  34.         {
  35.             get { return address; }
  36.             set
  37.             {
  38.                 if (address != value)
  39.                 {
  40.                     address = value;
  41.                     OnPropertyChanged("Address");
  42.                 }
  43.             }
  44.         }
  45.         public event PropertyChangedEventHandler PropertyChanged;
  46.         protected void OnPropertyChanged(string propertyName)
  47.         {
  48.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  49.         }
  50.     }
  51.     public class Address : INotifyPropertyChanged
  52.     {
  53.         private string city;
  54.         public string City
  55.         {
  56.             get { return city; }
  57.             set
  58.             {
  59.                 if (city != value)
  60.                 {
  61.                     city = value;
  62.                     OnPropertyChanged("City");
  63.                 }
  64.             }
  65.         }
  66.         public event PropertyChangedEventHandler PropertyChanged;
  67.         protected void OnPropertyChanged(string propertyName)
  68.         {
  69.             PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
  70.         }
  71.     }
  72. }
复制代码
在这个示例中,TextBox 的 Text 属性绑定到 Person 类中的 Address 对象的 City 属性。
2.2 数据上下文

数据上下文(DataContext)是WPF中一个紧张的概念,它指定了控件绑定的数据源。通过设置控件或其父容器的 DataContext 属性,可以简化绑定的设置。
结论

通过学习数据绑定的基础知识,我们可以更高效地开发WPF应用步调。数据绑定允许我们将UI元素和数据源连接起来,实现自动更新和显示。在本篇博客中,我们介绍了单向绑定和双向绑定的基本用法,以及绑定路径和数据上下文的概念。希望这些内容可以或许帮助你更好地理解和应用WPF的数据绑定功能。


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

王柳

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表