WPF的5种绑定模式
WPF的5种绑定模式
- OneWay(源变就更新目标属性)
- TwoWay(源变就更新目标并且目标变就更新源)
- OneTime(只根据源来设置目标,以后都不会变)
- OneWayToSource(与OneWay相反)
- Default(可以单向或双向,是靠被值定的源或目标是否有get或set来指定的)
演示效果
XAML
xml
<Window x:Class="WPF_CH07_Demo01.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:WPF_CH07_Demo01"
mc:Ignorable="d"
Name="MainWindow1"
Title="MainWindow" Height="450" Width="800">
<StackPanel>
<Slider Name="sb1" Minimum="0" Maximum="100" Value="50" Width="300" Height=" 30" SmallChange="1"/>
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="300"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Content="OneWay"/>
<TextBox Grid.Row="0" Grid.Column="1" Text="{Binding ElementName=sb1,Path=Value,Mode=OneWay}"/>
<Label Grid.Row="1" Grid.Column="0" Content="OneWayToSource"/>
<TextBox Grid.Row="1" Grid.Column="1" Text="{Binding ElementName=sb1,Path=Value,Mode=OneWayToSource}"/>
<Label Grid.Row="2" Grid.Column="0" Content="TwoWay"/>
<TextBox Grid.Row="2" Grid.Column="1" Text="{Binding ElementName=sb1,Path=Value,Mode=TwoWay}"/>
<Label Grid.Row="3" Grid.Column="0" Content="OneTime"/>
<TextBox Grid.Row="3" Grid.Column="1" Text="{Binding ElementName=sb1,Path=Value,Mode=OneTime}"/>
</Grid>
</StackPanel>
</Window>