如何解决数据网格中带有复选框的组合框 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));
}
}
}
现在它会像这样输出。
当我改变第一行的第二列时,整个第二列都是一样的,我想确保每行的第二列可以单独选择。
请任何人帮助加载组合框中的项目集合并更正我的代码。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。