Mantracker
Mantracker

Reputation: 613

Removing Duplicate DataTrigger Code from XAML

I am creating a lot of DataGrids in my WPF Application in the XAML file.

Generally, each DataGrid I make look like this:

<DataGrid x:Name="DamageTakenData" HorizontalAlignment="Left" VerticalAlignment="Top" Width="506" Height="172" Canvas.Left="30" Canvas.Top="228">
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Row}" Value="1">
                                <Setter Property="Foreground" Value="Red" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="2">
                                <Setter Property="Foreground" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="3">
                                <Setter Property="Foreground" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="4">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="5">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="6">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
         </DataGrid>

I have about 6 or 7 of those DataGrids, each of them in a different tab. Each DataGrid has the same DataGrid.CellStyle

<DataGrid x:Name="PlayerDeathData" HorizontalAlignment="Left" Margin="27,35,0,0" VerticalAlignment="Top" Height="155" Width="626" Grid.ColumnSpan="2">
                <DataGrid.CellStyle>
                    <Style TargetType="{x:Type DataGridCell}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Row}" Value="1">
                                <Setter Property="Foreground" Value="Red" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="2">
                                <Setter Property="Foreground" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="3">
                                <Setter Property="Foreground" Value="Orange" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="4">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="5">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Row}" Value="6">
                                <Setter Property="Foreground" Value="Green" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.CellStyle>
            </DataGrid>

How would I go about eliminating the duplicate code for specifying DataGrid.CellStyle?

Attempts

Looking at this question: Prevent Duplicate Code In XAML, I've tried something similar. I made a static resource:

<Window.Resources>
    <Style TargetType="{x:Type DataGridCell}" x:Key="DataGridStyle">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Row}" Value="1">
                <Setter Property="Foreground" Value="Red" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="2">
                <Setter Property="Foreground" Value="Orange" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="3">
                <Setter Property="Foreground" Value="Orange" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="4">
                <Setter Property="Foreground" Value="Green" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="5">
                <Setter Property="Foreground" Value="Green" />
            </DataTrigger>
            <DataTrigger Binding="{Binding Row}" Value="6">
                <Setter Property="Foreground" Value="Green" />
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

And then, I use the static resource like this:

<DataGrid x:Name="DamageDealtData" HorizontalAlignment="Left" VerticalAlignment="Top" Width="506" Height="163" Canvas.Left="30" Canvas.Top="35">
                <DataGridCell Style="{StaticResource DataGridStyle}"/>
            </DataGrid>

This approach doesn't work either. Because it seems to be creating only 1 DataGridCell and styling it.

How would I go about doing that?

Upvotes: 2

Views: 96

Answers (1)

Brandon Kramer
Brandon Kramer

Reputation: 1118

You have the resource correct, you are just applying it incorrectly.

It should be done like this:

<DataGrid x:Name="DamageDealtData" HorizontalAlignment="Left"
VerticalAlignment="Top" Width="506" Height="163" Canvas.Left="30"
Canvas.Top="35" DataGrid.CellStyle="{StaticResource DataGridStyle}">
    ...
</DataGrid>

When you used <DataGridCell Style="{StaticResource DataGridStyle}"/> you were creating a single DataGridCell and setting its style to the style you created. By setting DataGrid.CellStyle instead, you apply that style to ALL cells of the grid.

Upvotes: 2

Related Questions