元素之间的绑定。
TextBox
和Slider
之间的值绑定,默认是双向绑定的。
双向绑定,需要按 tab 键切换焦点以后才会有反应。
<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>
看这两行:
<Slider x:Name="slider" Grid.Row="0" Margin="10"/>
<TextBox Text="{Binding ElementName=slider, Path=Value}" Grid.Row="1" Margin="10"/>
默认是双向绑定的。
参考: https://www.bilibili.com/video/BV1mJ411F7zG?p=7&t=723