C#-WPF数据绑定基础(一)

时间:2023-03-09 00:17:04
C#-WPF数据绑定基础(一)

前言:WPF数据绑定技术有效的提高了程序的容错率,可以最大程度的保持程序的健壮性,从而降低程序在使用过程中崩掉的可能性。

接下来,我将分享一下我在写测量程序过程中所用到的数据绑定方面的知识

首先,我所用到的数据绑定的基本原理

C#-WPF数据绑定基础(一)

如上图所示,我们通过建立一个类,我们给它取名为视图模型,通过这个类里面的属性可以将我们的后台与界面实时的联系起来,以保证数据的实时更新,我们主要通过基类中的属性来进行数据绑定。

在界面设计代码中,我们用到Binding属性来进行数据绑定,代码如下所示

 1 <Window x:Class="GaussProj.AzimuthWindow"
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:GaussProj"
7 mc:Ignorable="d"
8 Title="坐标方位角计算" Height="380" Width="500">
9 <Window.Resources>
10 <Style TargetType="TextBox">
11 <Setter Property="Background" Value="AliceBlue"/>
12 <Setter Property="VerticalAlignment" Value="Center"/>
13 </Style>
14 <Style TargetType="TextBlock">
15 <Setter Property="Background" Value="AliceBlue"/>
16 <Setter Property="VerticalAlignment" Value="Center"/>
17 <Setter Property="HorizontalAlignment" Value="Right"/>
18 </Style>
19 </Window.Resources>
20 <Grid Margin="20">
21 <Grid.RowDefinitions>
22 <RowDefinition Height="40*"/>
23 <RowDefinition Height="40*"/>
24 <RowDefinition Height="50*"/>
25 <RowDefinition Height="30*"/>
26 </Grid.RowDefinitions>
27 <GroupBox Header="起点">
28 <Grid>
29 <Grid.ColumnDefinitions>
30 <ColumnDefinition Width="40*"/>
31 <ColumnDefinition Width="60*"/>
32 <ColumnDefinition Width="40*"/>
33 <ColumnDefinition Width="60*"/>
34 <ColumnDefinition Width="40*"/>
35 <ColumnDefinition Width="60*"/>
36 </Grid.ColumnDefinitions>
37 <TextBlock Text="点名:" Margin="2" Grid.Row="0" Grid.Column="0"/>
38 <TextBox x:Name="A_Name" Grid.Row="0" Grid.Column="1" Text="{Binding PA.Name}" Background="AliceBlue" Margin="3"/>
39
40 <TextBlock Text="X=" Margin="2" Grid.Row="0" Grid.Column="2"/>
41 <TextBox x:Name="AX_TextBox" Grid.Row="0" Grid.Column="3" Text="{Binding PA.X}" Background="AliceBlue" Margin="3"/>
42 <TextBlock Text="Y=:" Margin="2" Grid.Row="0" Grid.Column="4"/>
43 <TextBox x:Name="AY_TextBox" Grid.Row="0" Grid.Column="5" Text="{Binding PA.Y}" Background="AliceBlue" Margin="3"/>
44 </Grid>
45 </GroupBox>
46
47 <GroupBox Header="方向点" Grid.Row="1">
48 <Grid>
49 <Grid.ColumnDefinitions>
50 <ColumnDefinition Width="40*"/>
51 <ColumnDefinition Width="60*"/>
52 <ColumnDefinition Width="40*"/>
53 <ColumnDefinition Width="60*"/>
54 <ColumnDefinition Width="40*"/>
55 <ColumnDefinition Width="60*"/>
56 </Grid.ColumnDefinitions>
57 <TextBlock Text="点名:" Margin="2" Grid.Row="1" Grid.Column="0"/>
58 <TextBox x:Name="B_Name" Grid.Row="1" Grid.Column="1" Text="{Binding PB.Name}" Background="AliceBlue" Margin="3"/>
59
60 <TextBlock Text="X=" Margin="2" Grid.Row="1" Grid.Column="2"/>
61 <TextBox x:Name="BX_TextBox" Grid.Row="1" Grid.Column="3" Text="{Binding PB.X}" Background="AliceBlue" Margin="3"/>
62
63 <TextBlock Text="Y=:" Margin="2" Grid.Row="1" Grid.Column="4"/>
64 <TextBox x:Name="BY_TextBox" Grid.Row="1" Grid.Column="5" Text="{Binding PB.Y}" Background="AliceBlue" Margin="3"/>
65
66 </Grid>
67 </GroupBox>
68
69 <GroupBox Header="计算成果" Grid.Row="2">
70 <Grid>
71 <Grid.ColumnDefinitions>
72 <ColumnDefinition Width="105*"/>
73 <ColumnDefinition Width="120*"/>
74 </Grid.ColumnDefinitions>
75 <Grid.RowDefinitions>
76 <RowDefinition Height="30*"/>
77 <RowDefinition Height="30*"/>
78 </Grid.RowDefinitions>
79 <TextBlock d:Text="坐标方位角:" Name="AZ_Name" Text="{Binding AzName}" Grid.Row="0" Grid.Column="0" Margin="12"/>
80 <TextBox x:Name="Az" Grid.Row="0" Grid.Column="1" Text="{Binding AzValue}" Background="AliceBlue" Margin="3"/>
81
82 <TextBlock Text="距离:" Grid.Row="1" Grid.Column="0" Margin="12"/>
83 <TextBox x:Name="Distance" Grid.Row="1" Grid.Column="1" Text="{Binding Dist}" Background="AliceBlue" Margin="3"/>
84 </Grid>
85 </GroupBox>
86 <StackPanel Orientation="Horizontal" Grid.Row="3" HorizontalAlignment="Center">
87 <Button x:Name="Calculate" Content="计算" BorderThickness="3" BorderBrush="YellowGreen" Width="100" Click="Calculate_Click" Margin="10"/>
88 <Button x:Name="Exit" BorderThickness="3" BorderBrush="YellowGreen" Click="Exit_Click" Width="100" Content="关闭" Margin="10"/>
89 </StackPanel>
90 </Grid>
91 </Window>

数据绑定模型类的设计代码如下:

  1 {
2 /// <summary>
3 ///
4 /// </summary>
5 public class AzimuthWindowVM : NotifyPropertyObject
6 {
7 //public event PropertyChangedEventHandler PropertyChanged;
8 //public void RaisePropertyChanged(string propertyName)
9 //{
10 // if (PropertyChanged != null)
11 // {
12 // PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
13 // }
14 //}
15
16 private Point pA = new Point("A",0.0,0.0);
17 public Point PA
18 {
19 get { return pA; }
20 //set
21 //{
22 // pA = value;
23 // RaisePropertyChanged("PA");
24 // //if (PropertyChanged != null)
25 // //{
26 // // PropertyChanged.Invoke(this, new PropertyChangedEventArgs("AName"));
27 // //}
28 //}
29 }
30 //private double xA;
31
32 //public double XA
33 //{
34 // get { return xA; }
35 // set
36 // {
37 // xA = value;
38 // RaisePropertyChanged("XA");
39 // //if (PropertyChanged != null)
40 // //{
41 // // PropertyChanged.Invoke(this, new PropertyChangedEventArgs("XA"));
42 // //}
43 // }
44 //}
45
46 //private double yA;
47 //public double YA
48 //{
49 // get { return yA; }
50 // set
51 // {
52 // yA = value;
53 // RaisePropertyChanged("YA");
54 // //if (PropertyChanged != null)
55 // //{
56 // // PropertyChanged.Invoke(this, new PropertyChangedEventArgs("YA"));
57 // //}
58 // }
59 //}
60
61 private Point pB = new Point("B",1.0,1.0);
62 public Point PB
63 {
64 get { return pB; }
65 //set
66 //{
67 // pB = value;
68 // RaisePropertyChanged("PB");
69 // //if (PropertyChanged != null)
70 // //{
71 // // PropertyChanged.Invoke(this, new PropertyChangedEventArgs("BName"));
72 // //}
73 //}
74 }
75
76 //private double xB;
77
78 //public double XB
79 //{
80 // get { return xB; }
81 // set
82 // {
83 // xB = value;
84 // RaisePropertyChanged("XB");
85 // //if (PropertyChanged != null)
86 // //{
87 // // PropertyChanged.Invoke(this, new PropertyChangedEventArgs("XB"));
88 // //}
89 // }
90 //}
91
92 //public double yB;
93 //public double YB
94 //{
95 // get { return yB; }
96 // set
97 // {
98 // yB = value;
99 // RaisePropertyChanged("YB");
100 // //if (PropertyChanged != null)
101 // //{
102 // // PropertyChanged.Invoke(this, new PropertyChangedEventArgs("YB"));
103 // //}
104 // }
105 //}
106
107
108 private string azName = "A001->B004的坐标方位角";
109 public string AzName
110 {
111 get { return azName; }
112 set { azName = value; RaisePropertyChanged("AzName"); }
113 }
114
115 private String azValue;
116 public String AzValue
117 {
118 get { return azValue; }
119 set { azValue = value; RaisePropertyChanged("AzValue"); }
120 }
121 private double dist;
122 public double Dist
123 {
124 get { return dist; }
125 set { dist = value; RaisePropertyChanged("Dist"); }
126 }
127
128 public void Caculate()
129 {
130 var ad = FZY.SurMath.AzimuthDistance(PA.X, PA.Y, PB.X, PB.Y);
131 AzValue = FZY.SurMath.RADtoDMSString(ad.az);
132 Dist = ad.d;
133 AzName = $"{PA.Name}->{PB.Name}的坐标方位角";
134 }
135 }
136 }

很明显,这个类是从NotifyPropertyObject类继承过来的

 1 using System.ComponentModel;
2
3 namespace FZY
4 {
5 public class NotifyPropertyObject : INotifyPropertyChanged
6 {
7 public event PropertyChangedEventHandler PropertyChanged;
8 public void RaisePropertyChanged(string propertyName)
9 {
10 if (PropertyChanged != null)
11 {
12 PropertyChanged.Invoke(this, new PropertyChangedEventArgs(propertyName));
13 }
14 }
15 }
16 }

在这里,我们用到了INotifyPropertyChanged接口的一些功能。

总结:WPF数据绑定技术实质用到的就是C#中的属性的知识,我们通过set访问器来实时写入新值,这足以显示出属性的重要性,因此,熟练运用属性知识对数据绑定来说非常重要。