一、双向绑定
1.代码示例
<Window x:Class="学习.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:学习"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel><Slider x:Name="slider" Margin="10"/><TextBox Height="30" Margin="10" Text="{Binding ElementName=slider,Path=Value}"/><TextBox Height="30" Margin="10" Text="{Binding ElementName=slider,Path=Value}"/><TextBox Height="30" Margin="10" Text="{Binding ElementName=slider,Path=Value}"/></StackPanel></Grid>
</Window>
2.代码结果
二、单向绑定
1.代码示例
<Window x:Class="学习.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:学习"mc:Ignorable="d"Title="MainWindow" Height="450" Width="800"><Grid><StackPanel><Slider x:Name="slider" Margin="10"/><TextBox Height="30" Margin="10" Text="{Binding ElementName=slider,Path=Value,Mode=OneWay}"/> <!--单向绑定--><TextBox Height="30" Margin="10" Text="{Binding ElementName=slider,Path=Value,Mode=OneTime}"/><!--绑定第一个数值--><TextBox Height="30" Margin="10" Text="{Binding ElementName=slider,Path=Value,Mode=TwoWay}"/><!--双向绑定--><TextBox Height="30" Margin="10" Text="{Binding ElementName=slider,Path=Value,Mode=OneWayToSource}"/><!--单向绑定--></StackPanel></Grid>
</Window>
2.代码结果