王柳 发表于 2024-7-28 19:22:47

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

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

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

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

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

<Window x:Class="WpfApp.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="DataBinding Demo" Height="200" Width="300">
    <Grid>
      <TextBox Text="{Binding Name}" Width="200" Height="30" Margin="10"/>
    </Grid>
</Window>
using System.ComponentModel;
using System.Windows;

namespace WpfApp
{
    public partial class MainWindow : Window
    {
      public MainWindow()
      {
            InitializeComponent();
            DataContext = new Person { Name = "Alice" };
      }
    }

    public class Person : INotifyPropertyChanged
    {
      private string name;
      public string Name
      {
            get { return name; }
            set
            {
                if (name != value)
                {
                  name = value;
                  OnPropertyChanged("Name");
                }
            }
      }

      public event PropertyChangedEventHandler PropertyChanged;

      protected void OnPropertyChanged(string propertyName)
      {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
    }
}
在这个示例中,TextBox 的 Text 属性绑定到 Person 类的 Name 属性,当 Name 属性变化时,TextBox 会自动更新显示的内容。
1.2 双向绑定(TwoWay)

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

<Window x:Class="WpfApp.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="DataBinding Demo" Height="200" Width="300">
    <Grid>
      <TextBox Text="{Binding Name, Mode=TwoWay}" Width="200" Height="30" Margin="10"/>
      <Button Content="Update Name" Width="100" Height="30" Margin="10,50,0,0" Click="Button_Click"/>
    </Grid>
</Window>
using System.ComponentModel;
using System.Windows;

namespace WpfApp
{
    public partial class MainWindow : Window
    {
      public MainWindow()
      {
            InitializeComponent();
            DataContext = new Person { Name = "Alice" };
      }

      private void Button_Click(object sender, RoutedEventArgs e)
      {
            (DataContext as Person).Name = "Bob";
      }
    }

    public class Person : INotifyPropertyChanged
    {
      private string name;
      public string Name
      {
            get { return name; }
            set
            {
                if (name != value)
                {
                  name = value;
                  OnPropertyChanged("Name");
                }
            }
      }

      public event PropertyChangedEventHandler PropertyChanged;

      protected void OnPropertyChanged(string propertyName)
      {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
    }
}
在这个示例中,点击按钮会将 Person 类的 Name 属性更新为 “Bob”,同时 TextBox 中的文本也会自动更新。
2. 绑定路径和数据上下文

2.1 绑定路径

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

<Window x:Class="WpfApp.MainWindow"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      Title="DataBinding Demo" Height="200" Width="300">
    <Grid>
      <TextBox Text="{Binding Address.City}" Width="200" Height="30" Margin="10"/>
    </Grid>
</Window>
using System.ComponentModel;
using System.Windows;

namespace WpfApp
{
    public partial class MainWindow : Window
    {
      public MainWindow()
      {
            InitializeComponent();
            DataContext = new Person
            {
                Name = "Alice",
                Address = new Address { City = "New York" }
            };
      }
    }

    public class Person : INotifyPropertyChanged
    {
      private string name;
      private Address address;

      public string Name
      {
            get { return name; }
            set
            {
                if (name != value)
                {
                  name = value;
                  OnPropertyChanged("Name");
                }
            }
      }

      public Address Address
      {
            get { return address; }
            set
            {
                if (address != value)
                {
                  address = value;
                  OnPropertyChanged("Address");
                }
            }
      }

      public event PropertyChangedEventHandler PropertyChanged;

      protected void OnPropertyChanged(string propertyName)
      {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
    }

    public class Address : INotifyPropertyChanged
    {
      private string city;

      public string City
      {
            get { return city; }
            set
            {
                if (city != value)
                {
                  city = value;
                  OnPropertyChanged("City");
                }
            }
      }

      public event PropertyChangedEventHandler PropertyChanged;

      protected void OnPropertyChanged(string propertyName)
      {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
      }
    }
}
在这个示例中,TextBox 的 Text 属性绑定到 Person 类中的 Address 对象的 City 属性。
2.2 数据上下文

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

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

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
页: [1]
查看完整版本: C# WPF入门学习主线篇(二十四)—— 数据绑定基础