Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.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# 如何将动态创建的复选框链接到选中或未选中的事件?_C#_Wpf_Datagrid - Fatal编程技术网

C# 如何将动态创建的复选框链接到选中或未选中的事件?

C# 如何将动态创建的复选框链接到选中或未选中的事件?,c#,wpf,datagrid,C#,Wpf,Datagrid,我有一个Datagrid,在列标题中有一个动态创建的复选框。如何将此复选框链接到已选中和未选中事件,以便可以选中或取消选中标题下的整个列 private void gridView_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)

我有一个Datagrid,在列标题中有一个动态创建的复选框。如何将此复选框链接到已选中和未选中事件,以便可以选中或取消选中标题下的整个列

private void gridView_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
    {
        if (e.PropertyName == "code" && rdbCode.IsChecked == true)
        {
            e.Column.Header = "Acct Code";
        }
        else if (e.PropertyName == "code" && rdbPart.IsChecked == true)
        {
            e.Column.MaxWidth = 0;
        }

        if (e.PropertyName == "um")
        {
            e.Column.MaxWidth = 0;
        }

        if (e.PropertyName == "part" && rdbPart.IsChecked == true)
        {
            e.Column.Header = "Part ID";
        }
        else if (e.PropertyName == "part" && rdbCode.IsChecked == true)
        {
            e.Column.MaxWidth = 0;
        }

        if (e.PropertyName == "check")
        {
            CheckBox chk = new CheckBox();
            e.Column.Header = chk;
            chk.Content = "Update All";                                
        }
    }

这非常有效。

您需要订阅OnChecked事件(或它可能调用的任何内容),然后在网格中迭代并相应地进行处理
 private void gridView_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                             
    {
        if (e.PropertyName == "code" && rdbCode.IsChecked == true)
        {
            e.Column.Header = "Acct Code";
        }
        else if (e.PropertyName == "code" && rdbPart.IsChecked == true)
        {
            e.Column.MaxWidth = 0;
        }

        if (e.PropertyName == "um")
        {
            e.Column.MaxWidth = 0;
        }

        if (e.PropertyName == "part" && rdbPart.IsChecked == true)
        {
            e.Column.Header = "Part ID";
        }
        else if (e.PropertyName == "part" && rdbCode.IsChecked == true)
        {
            e.Column.MaxWidth = 0;
        }

        if (e.PropertyName == "check")
        {
            CheckBox chk = new CheckBox();
            e.Column.Header = chk;
            chk.Content = "Update All";
            chk.Checked += chk_Checked;
            chk.Unchecked += chk_Unchecked;
        }
    }