0
 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;
        }
    }

I know that I can add a checkbox to a Datagrid header with XAML, but can I add one with C# during the AutoGeneratingColumn event? I use the same Datagrid for different searches and populate the grid dynamically with different lists. I need to be able to add the "check all" checkbox to the header when the data is added, instead of being already formatted.

1 Answer 1

3

Probably it can be done like this:

private void DataGrid_OnAutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
{
    if (e.PropertyName == "Name")
    {
        e.Column.Header = new CheckBox { Content = "Check all" };
    }
}

The result:

The result

In general, all that we can do in xaml, also can be done in code

Sign up to request clarification or add additional context in comments.

1 Comment

This answered my question perfectly and was exactly what I was looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.