Chrisjan Lodewyks
Chrisjan Lodewyks

Reputation: 1327

WPF DataGrid Style in code behind

I have a xaml style for datagrids in my WPF application, I am now writing a custom control that inherits from DataGrid an would like to apply the following style in code behind:

<Style TargetType="DataGrid">

    <!-- Make the border and grid lines a little less imposing -->
    <Setter Property="BorderBrush" Value="#DDDDDD" />
    <Setter Property="HorizontalGridLinesBrush" Value="#DDDDDD" />
    <Setter Property="VerticalGridLinesBrush" Value="#DDDDDD" />

    <Setter Property="RowStyle">
        <Setter.Value>
            <Style TargetType="DataGridRow">
                <Style.Triggers>
                    <!-- Highlight a grid row as the mouse passes over -->
                    <Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="Background" Value="Lavender" />
                    </Trigger>
                </Style.Triggers>
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="CellStyle">
        <Setter.Value>
            <Style TargetType="DataGridCell">
                <Style.Triggers>
                    <!-- Highlight selected rows -->
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="Lavender" />
                        <Setter Property="BorderBrush" Value="Lavender" />
                        <Setter Property="Foreground" Value="Black" />
                    </Trigger>
                    <!--StartsEditingOnMouseOver-->
                    <!--<Trigger Property="IsMouseOver" Value="True">
                        <Setter Property="IsEditing" Value="True" />
                    </Trigger>-->
                </Style.Triggers>

                <EventSetter Event="PreviewMouseLeftButtonDown" Handler="DataGridCell_PreviewMouseLeftButtonDown" />
                <EventSetter Event="PreviewTextInput" Handler="DataGridCell_PreviewTextInput" />

                <!-- Add some padding around the contents of a cell -->
                <Setter Property="Padding" Value="4,3,4,3" />
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="DataGridCell">
                            <Border Padding="{TemplateBinding Padding}" 
                            Background="{TemplateBinding Background}">
                                <ContentPresenter />
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Setter.Value>
    </Setter>
</Style>

What I have so far is the following code:

static DionysusDataGrid()
{

  BorderBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata(ColorConverter.ConvertFromString("#FFDDDDDD") as Color?));
  HorizontalGridLinesBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata(ColorConverter.ConvertFromString("#FFDDDDDD") as Color?));
  VerticalGridLinesBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata(ColorConverter.ConvertFromString("#FFDDDDDD") as Color?));

}

But I have no idea how to do the same for the "RowStyle" property which also has a style in itself. And I am also getting the following error when setting the BorderBrushProperty:

Default value type does not match type of property 'BorderBrush'."

Can anyone help me out?

Thanx

UPDATE:

I resolved the error by updating the code to the following:

    static DionysusDataGrid()
{

  BrushConverter converter = new BrushConverter();

  BorderBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata((System.Windows.Media.Brush)converter.ConvertFromString("#FFDDDDDD")));
  HorizontalGridLinesBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata((System.Windows.Media.Brush)converter.ConvertFromString("#FFDDDDDD")));
  VerticalGridLinesBrushProperty.OverrideMetadata(typeof(DionysusDataGrid), new FrameworkPropertyMetadata((System.Windows.Media.Brush)converter.ConvertFromString("#FFDDDDDD")));

}

Upvotes: 4

Views: 9933

Answers (1)

Louis Kottmann
Louis Kottmann

Reputation: 16628

To make the Style in code behind, a few general rules apply:

Anything you type in XAML has an equivalent in good old C#:

<Style ...> is just System.Windows.Style. The same goes for Setter, Trigger, you name it.

The only gotcha comes from the ContentProperty attribute which is the default property assigned, for example when you do:

<TextBlock>My text here!</TextBlock>

It sets the TextBlock.Text property to "My text here!", because the TextBlock class is marked with the attribute [ContentProperty("Text")]

And lastly, you need to start from the most nested element when you build from C#:

<Style TargetType="DataGrid">
    <Setter Property="BorderBrush" Value="#DDDDDD" />
</Style>

Becomes:

var brushConverter = new BrushConverter();

var bbSetter = new Setter(
    DataGrid.BorderBrushProperty, 
    brushConverter.ConvertFromString("#FFDDDDDD"));

var style = new Style(typeof(DataGrid));    
style.Setters.Add(bbSetter);

From this you should be able to convert any XAML to C#,
It is useful to note, though, that you can't map any C# to XAML, for example you can't make a dynamic storyboard in XAML, but you can in C#.

Upvotes: 7

Related Questions