如何在Datagrid WPF c#中添加复选框 [英] How to Add Check Box in Datagrid WPF c#

查看:19
本文介绍了如何在Datagrid WPF c#中添加复选框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在数据网格视图中添加复选框.我已经编写了测试代码但失败了.我想要做的是在数据网格中添加一个复选框,其中包含我添加到其中的项目与全选和全选选项.

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.

我不知道该怎么做,所以我需要一些帮助.我很困惑,如果我们添加动态,我们将如何跟踪选中或未选中的复选框.

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的列:

And then to set the columns of your DataGrid in this way:

<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屋!

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