有一个复选框已绑定到布尔字段“IsOutsourcing”
<CheckBox x:Name="chkIsOutsourcing" IsChecked="{Binding IsOutsourcing, Mode=TwoWay}" />
我需要在选中另一个复选框时检查它。
<CheckBox x:Name="chkIsOption1" IsChecked="{Binding IsOption1, Mode=TwoWay}" />
如何使用XAML完成?
我们可以在这里使用多个元素进行绑定吗?
IsChecked="{Binding IsOutsourcing chkIsOption1, Mode=TwoWay}"
谢谢!
答案 0 :(得分:1)
这可以使用MultiBinding with MultiValueConverter完成。
<CheckBox x:Name="chkIsOutsourcing">
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource BooleanConverter}">
<Binding Path="IsOutSourcing" />
<Binding Path="IsChecked"
ElementName="chkIsOption1" />
</MultiBinding>
</CheckBox.IsChecked>
</CheckBox>
转换器,
public class BooleanConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool value1 = (bool)values[0];
bool value2 = (bool)values[1];
return value1 || value2;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}