多值绑定
代码
<Grid>
<Grid.Resources>
<local:MultipleBoolConverter x:Key="MultipleBoolConverter" />
</Grid.Resources>
<Grid.RowDefinitions>
<RowDefinition Height="*" />
<RowDefinition Height="*" />
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<CheckBox
x:Name="chkbox1"
Grid.Row="1"
Grid.Column="1"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="条件 1" />
<CheckBox
x:Name="chkbox2"
Grid.Row="1"
Grid.Column="2"
HorizontalAlignment="Left"
VerticalAlignment="Top"
Content="条件 2" />
<CheckBox
Grid.Row="2"
Grid.Column="1"
Content="结果"
IsHitTestVisible="false">
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource MultipleBoolConverter}">
<Binding ElementName="chkbox1" Path="IsChecked" />
<Binding ElementName="chkbox2" Path="IsChecked" />
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
</Grid>
public class MultipleBoolConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
var b1 = (bool)values[0];
var b2 = (bool)values[1];
return b1 && b2;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}