4thSpace 4thSpace - 7 months ago 55
C# Question

How to create generic editing style for DataGridCheckBoxColumn?

I've created the following style for a DataGrid DataGridCheckBoxColumn:

<Style x:Key="TextColumnEditStyle" TargetType="TextBox">

Then on the DataGrid:

<DataGridCheckBoxColumn EditingElementStyle="{StaticResource TextColumnEditStyle}"...>

That's work fine.

Is there a way to make the above a more generic style that targets DataGridCheckBoxColumn so I don't have to assign the style to each DataGridCheckBoxColumn?

I have tried the following but it had no effect:

<Style TargetType="DataGridCheckBoxColumn">

...and then removed the style assignment:

<DataGridCheckBoxColumn ...>


You can create a custom column with this style set to it, and then instead of adding DataGridCheckBoxColumn you can add your own custom column and that will have your style applied

Create a new column say MyColumn so MyColumn.xaml looks like

<DataGridCheckBoxColumn  x:Class="TestApp.MyColumn"
             EditingElementStyle="{StaticResource TextColumnEditStyle}"

and MyColumn.xaml.cs looks like

public partial class MyColumn : DataGridCheckBoxColumn
    public MyColumn()

and then your view that uses grid can use it like this

    <DataGrid ItemsSource="{Binding Items}">
            <local:MyColumn Binding="{Binding .}"/>

you can create TextColumnEditStyle in a resource that will be used by new custom column.