WPF 元素查找的方式绑定 Bonding

元素之间的绑定。

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

▲ 效果

▲ 绑定模式的枚举

双向绑定,需要按 tab 键切换焦点以后才会有反应(默认下是失去焦点才触发)。绑定值何时触发更改事件由属性 UpdateSourceTrigger 控制,如果立即生效可设置 UpdateSourceTrigger=PropertyChanged

<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
posted @ 2021-07-18 13:48  double64  阅读(201)  评论(0)    收藏  举报