简体   繁体   English

ComboBox WPF中的复选框

[英]CheckBoxes within a ComboBox WPF

您是谁在WPF中的组合框中实现复选框的。

Yes, this is possible. 是的,这是可能的。 I have done something similar with the ListView which is essentially the same control. 我已经完成了与ListView基本相同的控件的类似操作。 The trick here is to make it FAST . 这里的窍门是使其快速 It should not take 30 seconds or whatever if you have thousands of items. 如果您有成千上万的物品,则无需花费30秒的时间。

Now, how you implement it depends if you want to build a generic control or one specifically for this object type. 现在,如何实现它取决于要构建通用控件还是专门为此对象类型构建控件。 The reason being is that to do this fast, you need to know which property of the item object is the IsChecked. 原因是要快速执行此操作,您需要知道item对象的哪个属性是IsChecked。 In my control, I created a DP called IsCheckedPath so its in the spirit of WPF. 在我的控件中,我创建了一个名为IsCheckedPath的DP,因此本着WPF的精神。 The control would then auto bind the IsCheckedPath property of each object to the corresponding checkbox. 然后,该控件会将每个对象的IsCheckedPath属性自动绑定到相应的复选框。

When the master check box is toggled, you need to loop through the Items and set the property value. 切换主复选框后,您需要遍历项目并设置属性值。 You have 2 choices for that: 1) reflection or 2) compile an expression tree upon setting the IsCheckedPath DP. 为此,您有2个选择:1)反射或2)在设置IsCheckedPath DP时编译表达式树。

I did #2 since reflection will be too slow. 我做了#2,因为反射太慢了。

If you only care about this specific object and don't think you'll ever reuse the control, then you can skip both and just set the property. 如果您只关心此特定对象,并且认为您永远不会重用该控件,则可以跳过这两者,而只需设置属性即可。 Not a good idea IMO, I'd go the generic route :). IMO不是一个好主意,我会走通用路线:)。

Also, note, you can't take a "shortcut" and loop through the visuals and set the checkbox from the other side since WPF controls virtualize and check boxes will not be "alive" for items that are scrolled out of view. 另外,请注意,您不能采取“快捷方式”并在视觉效果中循环并从另一侧设置复选框,因为WPF控件虚拟化了,并且对于滚动到视图之外的项目,复选框不会“处于活动状态”。

Hello and welcome to WPF! 您好,欢迎来到WPF! For this situation, it looks like a MultiBinding is what you're looking for. 对于这种情况,您似乎正在寻找MultiBinding Since the master checkbox can change the individual checkboxes and vice-versa, the binding should be two-way. 由于主复选框可以更改单个复选框,反之亦然,因此绑定应该是双向的。 The MultiBinding would be applied to the master check box, like this: MultiBinding将应用于主复选框,如下所示:

<Checkbox x:Name="MasterCheckBox">
<Checkbox.IsChecked>
    <MultiBinding Converter="[Instance of your converter class]" Mode="TwoWay">
        <Binding Source="ConversationViewModel.ConversationValues" Convert="{StaticResource PersonFilterTypeToBoolConverter}" ConverterParameter="{x:Static shared:MethodType.Mobile}"/>
        <Binding Source="ConversationViewModel.ConversationValues" Convert="{StaticResource PersonFilterTypeToBoolConverter}" ConverterParameter="{x:Static shared:MethodType.Skype}"/>
        <Binding Source="ConversationViewModel.ConversationValues" Convert="{StaticResource PersonFilterTypeToBoolConverter}" ConverterParameter="{x:Static shared:MethodType.Landline}"/>
    </MultiBinding>
</Checkbox.IsChecked>

You will need to also create a converter. 您还需要创建一个转换器。 The article linked to above describes how to make a simple converter. 上面链接的文章介绍了如何制作一个简单的转换器。 In your case, since the binding is two-way, you need to handle both the Convert and ConvertBack methods. 在您的情况下,由于绑定是双向的,因此您需要同时处理Convert和ConvertBack方法。 The ConvertBack method runs when the master checkbox is changed by the user, so it will have to set all the bindings under the multibinding to true or false. 当用户更改主复选框时,将运行ConvertBack方法,因此必须将multibinding下的所有绑定设置为true或false。 The Convert method runs when one of the individual checkboxes changes. 当各个复选框之一更改时,将运行Convert方法。 In that case, in WPF, it's proper to set the IsChecked of the master checkbox to true if ALL individual boxes are checked, false if ALL individual boxes are unchecked, and if they are mixed, IsChecked should be null. 在这种情况下,在WPF中,如果选中了所有单个框,则将主复选框的IsChecked复选框设置为true是正确的;如果未选中所有单个框,则将false设置为false,如果混合使用,则IsChecked应该为null。 IsChecked can be null because the type of that property is a bool? IsChecked可以为null,因为该属性的类型是bool? (nullable bool), which is how three-state checkboxes work in WPF. (空布尔),这是WPF中的三态复选框的工作方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM