WPF | 跟着视频敲代码6 | 绑定 Binding
作者:互联网
https://www.bilibili.com/video/BV1nY411a7T8?p=6
笔记(控件与控件之间建立绑定关系而非通过事件来驱动):
<Window.Resources> <Style TargetType="TextBox"> <Setter Property="Height" Value="20"></Setter> <Setter Property="Margin" Value="5,0"></Setter> </Style> </Window.Resources> <Grid> <StackPanel> <Slider x:Name="sl1" Minimum="0" Maximum="100" ></Slider> <TextBox x:Name="tx1" Text="{Binding ElementName=sl1,Path=Value,Mode=Default}" ></TextBox> <!--Mode默认是双向绑定--> <TextBox x:Name="tx2" Text="{Binding ElementName=sl1,Path=Value,Mode=OneTime}" ></TextBox> <TextBox x:Name="tx3" Text="{Binding ElementName=sl1,Path=Value,Mode=OneWay}" ></TextBox> <TextBox x:Name="tx4" Text="{Binding ElementName=sl1,Path=Value,Mode=OneWayToSource}" ></TextBox> <TextBox x:Name="tx5" Text="{Binding ElementName=sl1,Path=Value,Mode=TwoWay}" ></TextBox> </StackPanel> </Grid>
原始(控件与控件之间建立绑定关系通过事件来驱动):
public partial class Demo6 : Window { public Demo6() { InitializeComponent(); } /// <summary> /// Slider 滑动 三个输入框显示对应数值 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Slider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e) { this.tx1.Text = sl1.Value.ToString(); this.tx2.Text = sl1.Value.ToString(); this.tx3.Text = sl1.Value.ToString(); } /// <summary> /// 修改第一个输入框文本值 Slider跟着联动 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void tx1_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e) { double v = 0; double.TryParse(tx1.Text, out v); sl1.Value = v; } }
<Window.Resources> <Style TargetType="TextBox"> <Setter Property="Height" Value="20"></Setter> <Setter Property="Margin" Value="5,0"></Setter> </Style> </Window.Resources> <Grid> <StackPanel> <Slider x:Name="sl1" Minimum="0" Maximum="100" ValueChanged="Slider_ValueChanged"></Slider> <TextBox x:Name="tx1" TextChanged="tx1_TextChanged"></TextBox> <TextBox x:Name="tx2"></TextBox> <TextBox x:Name="tx3"></TextBox> </StackPanel> </Grid>
标签:控件,Text,绑定,Binding,sl1,Value,Slider,WPF,tx1 来源: https://www.cnblogs.com/dongxizhen/p/16492650.html