如何绑定一个ObservableCollection<布尔>以复选框在WPF一个列表框 [英] How to bind an ObservableCollection<bool> to a Listbox of Checkboxes in WPF

查看:151
本文介绍了如何绑定一个ObservableCollection<布尔>以复选框在WPF一个列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我指出,我很新的C#和WPF前缀这个问题。

Let me prefix this question by stating that I'm very new to both C# and WPF.

我试图连接<$ C $的集合C>布尔值包含6复选框的容器,并有当按下一个按钮,这些值的状态储存。我假设有一个简单的方法来做到这一点,因为绑定复选框似乎是一个很自然的事情的集合,但到目前为止,我所看到的所有解决方案都显得过于复杂(例如: http://merill.net/2009/10/wpf-checked-listbox/ )。

I'm trying to connect a collection of Boolean values to a container containing 6 checkboxes, and have the state of these values stored when a button is pressed. I'm assuming there is an easy way to do this, since binding checkboxes to a collection of seems a very natural thing to do, but all the solutions I have seen so far have seemed overly complicated (example: http://merill.net/2009/10/wpf-checked-listbox/).

我通过修改列表框的数据模板创建复选框并设置的ItemsSource 的ListBox 的ObservableCollection ,但我的问题是,我不知道怎么绑定器isChecked 来,因为我试图将其绑定到集合中的实际的对象,而不是对象的属性。

I create the checkboxes by modifying the data template of a ListBox and set the ItemsSource of the ListBox to the ObservableCollection, but my problem is that I don't know what to bind the IsChecked to, since I'm trying to bind it to the actual object in the collection and not a property of the object.

推荐答案

使用器isChecked ={结合}直接收集的物品绑定。

Use IsChecked="{Binding}" to bind the item of the collection directly.

<ListBox ItemsSource="{Binding MyBooleanCollection}" >
    <ListBox.ItemTemplate>
        <DataTemplate>
            <CheckBox IsChecked="{Binding Mode=OneWay}"/>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>



然而这是不可能做到绑定到使用该方法的源。因为在复选框器isChecked 属性绑定不绑定的项目的索引。所以,它不能改变它的集合,但收集的唯一的项目。

However it is not possible to do bind to source with this method. Because the binding on the IsChecked property of the CheckBox doesn't the index of the item of the binding. So, it can't change the collection but only the item of the collection.

要得到周围的限制,您可以创建一个包装为布尔值:

To get around that limitation, you can create a wrapper to the Boolean value:

public class Wrapper<T> : INotifyPropertyChanged
{
    private T value;
    public T Value
    {
        get { return value; }
        set
        {
            {
                this.value = value;
                OnPropertyChanged();
            }
        }
    }

    public static implicit operator Wrapper<T>(T value)
    {
        return new Wrapper<T> { value = value };
    }
    public static implicit operator T(Wrapper<T> wrapper)
    {
        return wrapper.value;
    }

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
            handler(this, new PropertyChangedEventArgs(propertyName));
    }
}

下面是使用的为例:

public partial class MainWindow : Window
{
    public ObservableCollection<Wrapper<bool>> MyBooleanCollection { get; private set; }

    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;
        MyBooleanCollection = new ObservableCollection<Wrapper<bool>>() { false, true, true, false, true };
    }
}



而在XAML:

And in the XAML:

<CheckBox IsChecked="{Binding Value}"/>

这篇关于如何绑定一个ObservableCollection&LT;布尔&GT;以复选框在WPF一个列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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