4thSpace 4thSpace - 1 year ago 121
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 ...>

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download