C# Binding

  • 数据绑定要使用属性而非字段。使用字段,看不到任何反应,也不会抛出异常。
  • 如果要让属性更新时能够通知Binding,则需要对象实现INotifyPropertyChanged接口。

例1:使用 DataContext 。

 

 1 <Window x:Class="WpfApp_Test.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfApp_Test"
 7         mc:Ignorable="d"
 8         Title="MainWindow" Height="450" Width="800">
 9     <StackPanel>
10         <TextBox x:Name="txtBox1" Text="{Binding str}"/>        
11         <Button x:Name="btn1" Click="btn1_Click" Content="Click"/>
12     </StackPanel>
13 </Window>
14 
15 
16 public partial class MainWindow : Window,INotifyPropertyChanged
17 { 
18     //public string str = "haha";
19     public string str { get; set; } = "haha";
20     public MainWindow()
21     {
22         InitializeComponent();
23         DataContext = this;
24     }
25 
26     public event PropertyChangedEventHandler? PropertyChanged;
27 
28     private void btn1_Click(object sender, RoutedEventArgs e)
29     {
30         str += "x";
31         if (PropertyChanged != null)
32         {
33             PropertyChanged(this, new PropertyChangedEventArgs("Str"));
34         }
35     }
36 }
View Code

 使用DataContext 的机制方式

  1. 在后台代码中:
 public MainWindow()
 {
     InitializeComponent();
     DataContext = this;
 }

  2.在XMAL中通过StaticResource

 1 <Window x:Class="WpfApp_Test.MainWindow"
 2         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
 3         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
 4         xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
 5         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
 6         xmlns:local="clr-namespace:WpfApp_Test"
 7         mc:Ignorable="d"
 8         Title="MainWindow" Height="450" Width="800">
 9     <Window.Resources>
10         <local:Student x:Key="stu" Id="1" Name="John" Age="21"/>
11     </Window.Resources>
12     <StackPanel DataContext="{StaticResource stu}">
13         <TextBox x:Name="txtBox1" Text="{Binding Name}"/>        
14         <Button x:Name="btn1" Click="btn1_Click" Content="Click"/>
15     </StackPanel>
16 </Window>
View Code

 

 1     public partial class MainWindow : Window,INotifyPropertyChanged
 2     {       
 3         public MainWindow()
 4         {
 5             InitializeComponent();            
 6         }
 7 
 8         public event PropertyChangedEventHandler? PropertyChanged;
 9 
10         private void btn1_Click(object sender, RoutedEventArgs e)
11         {
12             (Resources["stu"] as Student).Name += "h";            
13         }
14     }
15 
16     public class Student : INotifyPropertyChanged
17     {
18         public int Id { get; set; }
19         private string name;
20 
21         public string Name
22         {
23             get { return name; }
24             set
25             {
26                 name = value;
27                 if (PropertyChanged != null)
28                 {
29                     PropertyChanged(this, new PropertyChangedEventArgs("Name"));
30                 }
31             }
32         }
33         public int Age { get; set; }
34 
35         public event PropertyChangedEventHandler? PropertyChanged;
36     }
View Code

 

 

posted @ 2025-06-09 11:40  竹楼风雨声  阅读(29)  评论(0)    收藏  举报