wpf中的复选框绑定

时间:2013-09-06 04:41:58

标签: wpf binding checkbox

Chekbox i want in following manner

有没有人帮我创建一个复选框列表呢?..我需要它在wpf和mvvm模式,绑定...项目复选框应该只在勾选标题复选框时启用..

我的代码为

我的xaml是

<ListBox ItemsSource="{Binding CheckBoxListItemCollection}">
            <ListBox.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type CheckBox}" ItemsSource="{Binding CheckBoxSubItems}">
                    <ListBox  ItemsSource="{Binding ItemHeader}"></ListBox>
                </HierarchicalDataTemplate>                
            </ListBox.ItemTemplate>
        </ListBox>

我有一个班级

public class CheckBoxListItems
    {
        public CheckBoxListItems()
        {
            CheckBoxSubItems = new ObservableCollection<CheckBox>();
            ItemHeader = new ObservableCollection<CheckBox>();
        }
        public ObservableCollection<CheckBox> ItemHeader { get; set; }
        public ObservableCollection<CheckBox> CheckBoxSubItems { get; set; }
    }

My Viewmodel

private List<CheckBoxListItems> _checkBoxListItemCollection;
CheckBoxListItemCollection = new List<CheckBoxListItems>();
public List<CheckBoxListItems> CheckBoxListItemCollection
        {
            get
            { return _checkBoxListItemCollection; }
            set
            {
                if (_checkBoxListItemCollection != value)
                {
                    _checkBoxListItemCollection = value;
                    OnPropertyChanged(new PropertyChangedEventArgs("CheckBoxItems"));
                }
            }
        }

我试过

<TreeView ItemsSource="{Binding CheckBoxListItemCollection}" >

        <DataTemplate >
            <ListBox ItemsSource="{Binding CheckBoxSubItems}"></ListBox>
        </DataTemplate>

        <HierarchicalDataTemplate  ItemsSource="{Binding CheckBoxSubItems}">
            <ListBox ItemsSource="{Binding ItemHeader}"></ListBox>
        </HierarchicalDataTemplate>
    </TreeView>

1 个答案:

答案 0 :(得分:0)

如果我理解正确,那么我相信您可以在CodeProject网站上的Working with Checkboxes in the WPF TreeView文章中找到答案。