MultiBinding
当一个控件的某个属性需要绑定到多个值的时候,需要使用MultiBinding.
例子1
一个文本显示Person的Name和Age
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}Name:{0},Age:{1}">
<Binding Path="Person.Name"></Binding>
<Binding Path="Person.Age"></Binding>
</MultiBinding>
</TextBlock.Text>
</TextBlock>
例子2
当1,2,3都被选中时,下面的红色框隐藏,不使用后台代码逻辑。
这个时候,使用MultiBinding+MultiValueConverter可实现需求
首先定义多值转换器:
public class ToVisibilityMultiValueConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
if (values == null || values.Length != 3) return Visibility.Visible;
if (values[0] == null || values[1] == null || values[2] == null) return Visibility.Visible;
return ((bool)values[0] && (bool)values[1] && (bool)values[2]) ? Visibility.Collapsed : Visibility.Visible;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
XAML如下:
<Window x:Class="MultiBindingTest.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:local="clr-namespace:MultiBindingTest"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
Title="MainWindow"
Width="300"
Height="250"
mc:Ignorable="d">
<Window.Resources>
<local:ToVisibilityMultiValueConverter x:Key="ToVisibilityMultiValueConverter" />
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="40" />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center" Orientation="Horizontal">
<CheckBox x:Name="chk1" Margin="5" Content="1" />
<CheckBox x:Name="chk2" Margin="5" Content="2" />
<CheckBox x:Name="chk3" Margin="5" Content="3" />
</StackPanel>
<Rectangle Grid.Row="1" Width="100" Height="100" Fill="Red">
<Rectangle.Visibility>
<MultiBinding Converter="{StaticResource ToVisibilityMultiValueConverter}">
<Binding ElementName="chk1" Path="IsChecked" />
<Binding ElementName="chk2" Path="IsChecked" />
<Binding ElementName="chk3" Path="IsChecked" />
</MultiBinding>
</Rectangle.Visibility>
</Rectangle>
</Grid>
</Window>
MultiTrigger
与Trigger属性对应
当一个或多个控件本身的其他属性的值共同达到某一个条件时,触发一组属性变化。
使用场景:例如鼠标在按钮上且鼠标按下时,按钮的颜色为蓝色文章来源:https://www.toymoban.com/news/detail-446166.html
MultiDataTrigger
与DataTrigger对应
当一个或多个DataContext中的值达到某一个条件来触发操作时,需要使用MutiDataTrigger.
使用场景:
例如 一个按钮的DataContext中有一个对象Person,当Person的Name为“Tony”且Age为20时,按钮的背景色为黄色文章来源地址https://www.toymoban.com/news/detail-446166.html
<MultiTrigger>
<MultiTrigger.Conditions>
<Condition Property="IsMouseOver" Value="true"></Condition>
<Condition Property="IsPressed"
Value="true"></Condition>
</MultiTrigger.Conditions>
<MultiTrigger.Setters>
<Setter Property="Background" Value="blue"></Setter>
</MultiTrigger.Setters>
</MultiTrigger>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding Person.Name}"
Value="Tony"></Condition>
<Condition Binding="{Binding Person.Age}"
Value="20"></Condition>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Background"
Value="Yellow"></Setter>
</MultiDataTrigger.Setters>
</MultiDataTrigger>
到了这里,关于WPF 多值绑定(MultiBinding)与多属性触发器(MultiTrigger)与多数据触发器(MultiDataTrigger)的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!