将标志枚举绑定到包含复选框的列表框 [英] Bind Flag enums to listbox containing checkboxes

查看:47
本文介绍了将标志枚举绑定到包含复选框的列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有flag enum这么说-

I have flag enum say this -

[Flags]
public enum Department
{
    None = 0,
    A = 1,
    B = 2,
    C = 4,
    D = 8
}

我想在视图上显示该枚举的值.我想创建一个列表框并将其源绑定到该枚举 List< Department>的集合.部门.一切工作都很好,直到我想到有一个绑定到我的Viewmodel上的属性的复选框-

I want to show values of this enum on view. I thought of creating a listbox and binding its source to the collection of this enum List<Department> Departments. All works so good until i thought of having a checkbox which binds to a property on my Viewmodel -

public Department SelectedDepartments { get; set; }

此处的解决方案 http://compilewith.net/2008/12/wpf-flagsenumvalueconverter.html 为将枚举值绑定到复选框提供了优雅的解决方案,但是它具有一个限制,即创建等于列表中枚举值数量的复选框.但是,就我而言,由于我的Enum包含20个值(因此,UI上有20个复选框),我无法在UI上放置如此多的复选框.

The solution here http://compilewith.net/2008/12/wpf-flagsenumvalueconverter.html provides elegant solution for binding enum values to checkboxes but its have one limitation of creating checkboxes equal to the number of enum values in list. But, in my case i can't afford of having so many checkboxes lying on my UI since my Enum contains 20 values (so that means having 20 checkboxes on UI).

我尝试使用 MultiBindingConverter ,但是在 ConvertBack 方法中失败.我想将复选框的状态与属性SelectedDepartments绑定在一起.假设属性值是"A | B",则应选中A和B复选框,而C和D应该保持未选中状态.

I tried using MultiBindingConverter but that fails in ConvertBack Method. I want to bind the state of checkboxes with property SelectedDepartments. Say if property value is "A | B" then A and B checkbox should be checked whereas C and D should remain unchecked.

推荐答案

我认为不使用一些后台代码就无法做到这一点.

I don't think there's a way of doing this without using some code-behind.

我采用了上面链接的示例解决方案,从MainWindow.xaml中删除了所有CheckBox,在MainWindow.xaml.cs中添加了以下方法,并从 MainWindow 构造函数中对其进行了调用:

I took the sample solution you linked to above, removed all of the CheckBoxes from MainWindow.xaml, added the following method to MainWindow.xaml.cs and called it from the MainWindow constructor:

    private void AddCheckBoxes()
    {
        var converter = new FlagsEnumValueConverter();
        foreach (Department dept in Enum.GetValues(typeof(Department)))
        {
            if (dept != Department.None)
            {
                var binding = new Binding()
                {
                    Path = new PropertyPath("Department"),
                    Converter = converter,
                    ConverterParameter = dept
                };

                var checkBox = new CheckBox() { Content = dept.ToString() };
                checkBox.SetBinding(CheckBox.IsCheckedProperty, binding);
                DepartmentsPanel.Children.Add(checkBox);
            }
        }
    }

此方法完成所有复选框的创建工作,除了 None 之外,每个复选框均用于每个命名的枚举常量.然后,我可以将其他部门添加到 Department 枚举中,重新运行解决方案,并查看新添加部门的其他复选框.

This method does the work of creating all of the checkboxes, one for each named enum constant apart from None. I could then add further departments to the Department enum, rerun the solution and see additional checkboxes for the newly-added departments.

对于此解决方案,我还需要进行一些较小的更改才能使其完全正常工作.您可能需要也可能不需要对代码进行这些更改.首先,我使 DataObject 类实现了 INotifyPropertyChanged .其次,我在MainWindow.xaml中重写了XAML,如下所示:

There were a few further minor changes that I had to make to this solution to get it working completely. You may or may not need to make these changes to your code. Firstly, I made the DataObject class implement INotifyPropertyChanged. Secondly, I rewrote the XAML in MainWindow.xaml as follows:

<StackPanel>
    <StackPanel x:Name="DepartmentsPanel" />
    <TextBlock Margin="5,20,0,0">
        <TextBlock Text="Raw Value:" FontWeight="Bold" />
        <TextBlock Text="{Binding Department}" />
    </TextBlock>
</StackPanel>

(基本上,我将现有的DepartmentsPanel包裹在另一个StackPanel中,并将原始值"显示移动到该外部StackPanel中.)最后,我设置整个MainWindow的DataContext,而不是 DepartmentsPanel 到创建的 DataObject .要使原始值"显示正常工作,必须执行此步骤.

(Basically, I wrapped the existing DepartmentsPanel in another StackPanel and moved the 'Raw Value' display into this outer StackPanel.) Finally, I set the DataContext of the whole MainWindow, rather than the DataContext of the DepartmentsPanel, to the DataObject created. This step was necessary to make the 'Raw Value' display work.

这篇关于将标志枚举绑定到包含复选框的列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆