数据网格中带有复选框的组合框 XAML 代码

问题描述

我需要在 WPF 的 DataGrid 中显示带有复选框选项的组合框。我试过下面的代码

XAML 代码

<DataGrid Name="DG1"  AutoGenerateColumns="False" >
            <DataGrid.Columns>
                <DataGridTextColumn Header="Index" Binding="{Binding index}"/>
                
                <DataGridTemplateColumn Header="Date" Width="200">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <ComboBox Text="{Binding SelectedText,ElementName=this}" ItemsSource="{Binding combos,ElementName=this}"
                                      IsEditable="True">
                                <ComboBox.ItemTemplate>
                                    <DataTemplate DataType="{x:Type local:combocheck}">
                                        <StackPanel Orientation="Horizontal">
                                            <CheckBox IsChecked="{Binding IsChecked}" Content="{Binding type.name}" Width="100"/>
                                        </StackPanel>
                                    </DataTemplate>
                                </ComboBox.ItemTemplate>
                            </ComboBox>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

CS 代码

public partial class _0722 : Window,INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(name));
            }
        }

        private string _text;

        public string SelectedText
        {
            get { return _text; }
            set { _text = value;
                NotifyPropertyChanged("SelectedText");
            }
        }

        private ObservableCollection<combocheck> _types;

        public ObservableCollection<combocheck> combos
        {
            get 
            {
                if (_types == null)
                {
                    _types = new ObservableCollection<combocheck>();
                    _types.CollectionChanged += (sender,e) =>
                    {
                        if (e.OldItems != null)
                        {
                            foreach (combocheck item in e.OldItems)
                            {
                                item.PropertyChanged -= ItemPropertyChanged;
                            }
                        }
                        if (e.NewItems != null)
                        {
                            foreach (combocheck item in e.NewItems)
                            {
                                item.PropertyChanged += ItemPropertyChanged;
                            }
                        }
                    };
                }
                return _types;
            }
        }

        private ObservableCollection<gridlistitem> _gridlist;

        public ObservableCollection<gridlistitem> gridlist
        {
            get { return _gridlist; }
            set { _gridlist = value; }
        }


        public _0722()
        {
            InitializeComponent();
            combos.Add(new combocheck(false,new Type() { name = "Type1" }));
            combos.Add(new combocheck(false,new Type() { name = "Type2" }));
            combos.Add(new combocheck(false,new Type() { name = "Type3" }));
            combos.Add(new combocheck(false,new Type() { name = "Type4" }));
            combos.Add(new combocheck(false,new Type() { name = "Type5" }));
            combos.Add(new combocheck(false,new Type() { name = "Type6" }));
            gridlist = new ObservableCollection<gridlistitem>();
            gridlist.Add(new gridlistitem(0,new combocheck(false,new Type() { name = "Type1" })));
            gridlist.Add(new gridlistitem(1,new Type() { name = "Type2" })));
            this.DG1.ItemsSource = gridlist;
        }

        private void ItemPropertyChanged(object sender,PropertyChangedEventArgs e)
        {
            if (e.PropertyName == "IsChecked")
            {
                combocheck c = sender as combocheck;
                if (c != null)
                {
                    IEnumerable<combocheck> cs = combos.Where(b => b.IsChecked == true);
                    StringBuilder builder = new StringBuilder();
                    foreach (combocheck item in cs)
                    {
                        builder.Append(item.type.name + " ");
                    }
                    SelectedText = builder == null ? string.Empty : builder.ToString();
                }
            }
        }

    }
    public class gridlistitem
    {
        public int index { get; set; }
        public combocheck combocheckitem { get; set; }
        public gridlistitem(int _index,combocheck comche)
        {
            this.index = _index;
            this.combocheckitem = comche;
        }
    }
    public class combocheck : ObservableObject
    {
        public Type type { get; set; }
        private bool _ischecked;

        public bool IsChecked
        {
            get { return _ischecked; }
            set { _ischecked = value;
                NotifyPropertyChanged("IsChecked");
            }
        }
        public combocheck(bool ischecked,Type _type)
        {
            this.IsChecked = ischecked;
            this.type = _type;
        }
    }
    public class Type
    {
        public string name { get; set; }
    }
    public class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void NotifyPropertyChanged(string name)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this,new PropertyChangedEventArgs(name));
            }
        }
    }

现在它会像这样输出

enter image description here

enter image description here

当我改变第一行的第二列时,整个第二列都是一样的,我想确保每行的第二列可以单独选择。

请任何人帮助加载组合框中的项目集合并更正我的代码

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...