首先 - 是的,你可以通过使用MultiBinding
。 其次 - 如果你有3种样式的checkBox(首先是allChecked,第二次是allcheck,第三次是一些选中的,一些是未选中的),你可以编写一种样式,将其他人与触发器联合起来,分配到IsChecked
属性。 我为你写了一些例子。
<StackPanel>
<CheckBox Name="cb1" IsChecked="{Binding Is1}" />
<CheckBox Name="cb2" IsChecked="{Binding Is2}" />
<CheckBox Name="cb3" IsChecked="{Binding Is3}" />
<CheckBox Name="cbMaster" Content="Master">
<CheckBox.Resources>
<local:CheckBoxConverter x:Key="cbConv" />
</CheckBox.Resources>
<CheckBox.IsChecked>
<MultiBinding Converter="{StaticResource cbConv}">
<Binding Path="Is1" />
<Binding Path="Is2" />
<Binding Path="Is3" />
</MultiBinding>
</CheckBox.IsChecked>
<CheckBox.Style>
<Style TargetType="CheckBox">
<Style.Triggers>
<Trigger Property="IsChecked" Value="{x:Null}">
<Setter Property="Foreground" Value="Red" />
</Trigger>
<Trigger Property="IsChecked" Value="true">
<Setter Property="Foreground" Value="Green" />
</Trigger>
<Trigger Property="IsChecked" Value="false">
<Setter Property="Foreground" Value="Black" />
</Trigger>
</Style.Triggers>
</Style>
</CheckBox.Style>
</CheckBox>
</StackPanel>
转换器看起来像那样。
public class CheckBoxConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
bool isChecked = values.Contains(true);
bool isUnchecked = values.Contains(false);
if (isChecked && isUnchecked)
{
// some checked and uncheked
return null;
}
else if (isChecked)
{
return true;
}
return false;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, System.Globalization.CultureInfo culture)
{
int count = targetTypes.Length;
object[] result = new object[count];
if ((bool)value == true)
{
for (int i = 0; i < count; i++)
{
result[i] = true;
}
}
else
{
for (int i = 0; i < count; i++)
{
result[i] = false;
}
}
return result;
}
}
ViewModel看起来像那样。
public class CheckBoxViewModel : INotifyPropertyChanged
{
private bool _is1;
public bool Is1
{
get { return _is1; }
set { _is1 = value; NotifyPropertyChanged("Is1"); }
}
...
}