5、标记拓展

标记拓展,是一种特殊的赋值方式。有一对大括号{ }

<StackPanel Background="LightSlateGray">
        <TextBox x:Name="textBox1" Text="{Binding ElementName=slider1,Path=Value,Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}" Margin="5"/>
        <Slider x:Name="slider1" Margin="5" Minimum="0" Maximum="100"/>
</StackPanel>

TextBox的简单写法(Binding类的构造器本身可以接收Path的参数,所以可省略)

<TextBox x:Name="textBox1" Text="{Binding Value, ElementName=slider1,UpdateSourceTrigger=PropertyChanged}" Margin="5"/>

Model——控制Binding数据流向,值有TwoWay、OneWay、OneTime、OneWayToSource和Default。Default会根据目标实际情况来确定,如果是可以编辑的(TextBox的Text属性),Default就采用双向模式。如果是不可编辑(TextBlock),就使用单向模式。

UpdateSourceTrigger——数据更新,值有PropertyChanged、LostFous、Explicit和Default。PropertyChanged使Slider随输入值的变化而变化。

Path——路径,众多属性值中指定Binding关注的属性值

Binding类可以让控件之间关联起来,下图slider1与textBox1相互影响,textBox2识别textBox1字符串长度。

 标记拓展类的类名都是以Extension为后缀,只不过可以省略不写,如Text="{x:Static ...}"与Text="{x:StaticExtension ...}"等价

原文地址:https://www.cnblogs.com/xixixing/p/10979499.html