![Binding介绍 Binding介绍](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
一.Binding的源与路径
在大多数情况下Binding的源是逻辑层的对象,但有时候为了让UI元素产生一些联动效果也会使用Binding在控件间建立关联,
下面的代码是把一个TextBox的Text属性关联在了Slider的Value上
<Window x:Class="MyBinding.WindowBinding" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="WindowBinding" Height="150" Width="300"> <Grid> <StackPanel> <TextBox x:Name="textBox1" Text="{Binding Path=Value,ElementName=Slider1}" BorderBrush="Black" Margin="5" Height="30" VerticalContentAlignment="Center"/> <Slider x:Name="Slider1" Maximum="100" Minimum="0" Margin="5"/> </StackPanel> </Grid> </Window>
与之等价的C#代码是:
Binding binding = new Binding("Value") { Source = Slider1 }; this.textBox1.SetBinding(TextBox.TextProperty, binding);
二.控制Binding的方向及数据更新
Binding在源与目标之间架起了沟通的桥梁,默认情况下数据既能通过Binding送达目标,也能够从目标返回源(收集用户对数据的修改)。有时候数据只需要展示给用户
、不允许用户修改,这时候可以把Binding模式更改为从源向目标的单向沟通,Binding还支持从目标向源的单向沟通以及只在Binding关系确立时读取一次数据。
控制Binidng数据流向的属性是Mode,它的类型是BindingMode枚举。BindingMode可取值为TwoWay、OneWay、OnTime、OneWayToSource和Default。这里
的Default值是指Binding的模式会根据目标的实际情况来确定,比如若是可编辑的(TextBox.Text属性),Default就采用双向模式、若是只读的(TextBlock.Text)则采用单向模式
当我们拖动Slider的手柄时,TextBox里就会显示出Slider当前的值;如果我们在TextBox里输入一个恰当的值,然后按一下Tab键,让焦点离开TextBox,则Slider的手柄会跳到
相应的值那里,为什么一定要在TextBox失去焦点之后Slider的值才会改变?这就引出了Binding的另一个属性——UpdateSourceTrigger,它的类型是UpdateSourceTrigger枚举,
可取值为PropertyChanged、LostFocus、Explicit和Default。对于TextBox默认值Default的行为与LostFocus一致,我们只要把这个属性改为PropertyChanged,则Slider的手柄
就会随着我们在TextBox里输入而改变位置
三.使用ObjectDataProvider对象作为Binding的Source
由于一个类的所有数据都使用属性暴露出来,比如我们需要的数据可能是方法的返回值,这时候就需要使用ObjectDataProvider来包装作为Binding源的数据对象了
如下:
public class Calculator { //加法 public string Add(string arg1, string arg2) { ; ; ; if(double.TryParse(arg1, out x)&& double.TryParse(arg2,out y)) { z = x + y; return z.ToString(); } return "Input Error!"; } }
<Window x:Class="MyBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="180" Width="300" WindowStartupLocation="CenterScreen"> <Grid> <StackPanel Background="LightBlue"> <TextBox x:Name="textBoxArg1" Margin="5" Height="30"/> <TextBox x:Name="textBoxArg2" Margin="5" Height="30"/> <TextBox x:Name="textBoxResult" Margin="5" Height="30"/> </StackPanel> </Grid> </Window>
这个程序实现的功能是上面两个TextBox输入数字后,第3个TextBox能实时地显示数字的和
private void SetBinding() { //创建并配置ObjectDataProvider对象 ObjectDataProvider odp = new ObjectDataProvider(); odp.ObjectInstance = new Calculator(); odp.MethodName = "Add"; odp.MethodParameters.Add("); odp.MethodParameters.Add("); //以ObjectDataProvider对象为Source创建Binding Binding bindingToArg1 = new Binding("MethodParameters[0]") { Source = odp, BindsDirectlyToSource = true, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }; Binding bindingToArg2 = new Binding("MethodParameters[1]") { Source = odp, BindsDirectlyToSource = true, UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged }; Binding bindingToResult = new Binding(".") { Source = odp }; //将Binding关联到UI元素上 this.textBoxArg1.SetBinding(TextBox.TextProperty, bindingToArg1); this.textBoxArg2.SetBinding(TextBox.TextProperty, bindingToArg2); this.textBoxResult.SetBinding(TextBox.TextProperty, bindingToResult); }
四.使用Binding的RelativeSource
当一个Binding有明确的数据来源时我们可以通过为Source或ElementName赋值的办法让Binding与之关联,有时候我们不能确定作为Souce的对象叫什么,但知道它作为Binding目标的对象在UI布局上有
相对关系,比如控件自己关联自己的某个数据、关联自己某级容器的数据,这时候我们就要使用Binding的RelativeSource属性
<Window x:Class="MyBinding.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="180" Width="300" WindowStartupLocation="CenterScreen"> <Grid x:Name="g1" Background="Red" Margin="10"> <DockPanel x:Name="d1" Background="Orange" Margin="10"> <Grid x:Name="g2" Background="Yellow" Margin="10"> <DockPanel x:Name="d2" Background="LawnGreen" Margin="10"> <TextBox x:Name="textBox1" FontSize="24" Margin="10" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Grid},AncestorLevel=1},Path=Name}"/> </DockPanel> </Grid> </DockPanel> </Grid> </Window>
AncestorLevel属性指的是Binding目标控件作为起点的层级偏移量——d2的偏移量是1、g2的偏移量是2,以此类推,AncestorType属性告诉Binding寻找哪个类型的对象作为自己的源,
不是这个类型的对象会被跳过,上面这段代码的意思是告诉Binding从自己的第一层依次向外找,找到第一个Grid类型对象后把它当做自己的源。