如何添加复选框Datagrid的WPF C# [英] How to Add Check Box in Datagrid WPF c#
问题描述
您好我想添加复选框view.i已经writen测试代码,但没有数据网格。
我所试图做的是在DataGrid中添加一个复选框与我添加到它的项目
与全选,然后选择无选项。
Hi i want to add check box in datagrid view.i have writen test code but fails. What i am trying to do is Add a CheckBox in datagrid with the items i add into it with select all and select none option.
我DONOT知道该怎么做,所以我需要一些help.I感到混淆的事情,如果我们再添加动态会如何,我们跟踪它的复选框被选中或取消检查。
I donot know how to do it so i need some help.I am confused with a thing that if we add then dynamically how would we track which checkbox was checked or un checked.
我当前的代码
public partial class MainWindow : Window
{
List<checkedBoxIte> item = new List<checkedBoxIte>();
public MainWindow()
{
InitializeComponent();
for (int i = 0; i < 5; i++)
{
checkedBoxIte ite = new checkedBoxIte();
ite.sr = i.ToString();
ite.ch = new CheckBox();
item.Add(ite);
}
dataGrid1.ItemsSource = item
}
}
public class checkedBoxIte
{
public string sr {get;set;}
public CheckBox ch { get; set; }
}
但我知道这是添加复选框,这样愚蠢的事情,但它是只是一种尝试
上面的类包含两个属性以后,将有更多的,但都将是字符串
but i know it is stupidest thing to add checkbox like this but it was just a try Above class contains two attributes later on it would have more but all will be strings
推荐答案
WPF没有按知道如何对付你的 checkedBoxIte
项目。我建议你改变你的类如下:
WPF doesn't know how to deal with your checkedBoxIte
items. I suggest you to change your class as follows:
public class checkedBoxIte
{
public string MyString {get;set;}
public bool MyBool { get; set; }
}
然后设置你的 DataGrid的列
是这样的:
<DataGrid AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTextColumn Header="MyString" Binding="{Binding MyString}" />
<DataGridCheckBoxColumn Header="MyBool" Binding="{Binding MyBool}" />
</DataGrid.Columns>
</DataGrid>
现在您可以设置的ItemsSource
:
for (int i = 0; i < 5; i++)
{
checkedBoxIte ite = new checkedBoxIte();
ite.MyString = i.ToString();
item.Add(ite);
}
dataGrid1.ItemsSource = item;
这篇关于如何添加复选框Datagrid的WPF C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!