WPF|元素查找的方式Bonding

元素之间的绑定。

TextBoxSlider之间的值绑定,默认是双向绑定的。

▲效果

▲ 效果

▲ 绑定模式的枚举

▲ 绑定模式的枚举

双向绑定,需要按 tab 键切换焦点以后才会有反应。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<Window x:Class="WpfApp1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WpfApp1"
mc:Ignorable="d"
Title="MainWindow" Height="200" Width="380"
WindowStartupLocation="CenterScreen"
>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="50*"/>
</Grid.RowDefinitions>
<Slider x:Name="slider" Grid.Row="0" Margin="10"/>
<TextBox Text="{Binding ElementName=slider, Path=Value}" Grid.Row="1" Margin="10"/>
</Grid>
</Window>

看这两行:

1
2
<Slider x:Name="slider" Grid.Row="0" Margin="10"/>
<TextBox Text="{Binding ElementName=slider, Path=Value}" Grid.Row="1" Margin="10"/>

默认是双向绑定的。


感谢支持!