Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在Datagrid WPF c中添加复选框_C#_Wpf_Datagrid_Checkbox - Fatal编程技术网

C# 如何在Datagrid WPF c中添加复选框

C# 如何在Datagrid WPF c中添加复选框,c#,wpf,datagrid,checkbox,C#,Wpf,Datagrid,Checkbox,您好,我想在datagrid视图中添加复选框。我已编写测试代码,但失败。 我试图做的是在datagrid中添加一个复选框,其中包含我添加到其中的项 使用全选和全选选项 我不知道怎么做,所以我需要一些帮助。我很困惑,如果我们动态添加,那么我们将如何跟踪哪个复选框被选中或未选中 我有最新的密码 public partial class MainWindow : Window { List<checkedBoxIte> item = new List<

您好,我想在datagrid视图中添加复选框。我已编写测试代码,但失败。 我试图做的是在datagrid中添加一个复选框,其中包含我添加到其中的项 使用全选和全选选项

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

我有最新的密码

    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; }
    }
但我知道像这样添加复选框是最愚蠢的事情,但这只是一次尝试
上面的类包含两个属性,以后会有更多属性,但都是字符串,WPF不知道如何处理checkedBoxIte项。我建议你改变你的课程如下:

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>

它工作得很好,但我有一些问题,比如首先有4列出现,表示重复2。我们如何触发事件或检查这是哪个复选框?嗯,我认为有四列是不正常的。您确定已将AutoGenerateColumns设置为False吗?这是真的。正在工作。对于第二部分,我可以做什么检测选中的行将处理程序附加到DataGridCellEditEnding,然后从EventArgs的row属性提取行。或者,您可以在类上实现INotifyPropertyChanged,然后查看何时直接从该类中选中复选框。
for (int i = 0; i < 5; i++)
{
    checkedBoxIte ite = new checkedBoxIte();
    ite.MyString = i.ToString();
    item.Add(ite);
}
dataGrid1.ItemsSource = item;