WPF 元素查詢的方式繫結 Bonding
阿新 • • 發佈:2021-07-18
元素之間的繫結。
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