2015-10-29 46 views
0

我已经为我的应用程序设计了一些自定义复选框设计,下面是它们的图像。应该很清楚,但排在第一位的style1(全部被选中)。WPF:复选框选中所有其他复选框并取消勾选,视觉差异

第二个只是一个复选框,可以说style2。

但是第三个有让我们说style3,因为这将显示它的一些儿童复选框被选中但不是全部,点击这将取消选中/检查它们。

我有两个问题。

  1. 是否存在,以便有一个主复选框,将取消选中,并检查它的孩子们只使用XAML这些复选框绑在一起的方式,否则我将不得不使用C#命令?

  2. 这是主要问题,当所有复选框被选中并且只有一些被选中时,给主复选框一个不同样式的最佳方式是什么?

enter image description here

回答

0

首先 - 是的,你可以通过使用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"); } 
    } 
    ... 
} 
相关问题