Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DataGridCell.Template Style overriding IsSelected Trigger

Why does the IsSelected Trigger below only work when Template Setter is absent from the code? How can I get both to function as expected?

I have the following in my Application.Resources tag;

<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>

    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <ContentPresenter VerticalAlignment="Center"></ContentPresenter>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
like image 877
Stafford Williams Avatar asked Nov 17 '10 06:11

Stafford Williams


1 Answers

When you re-template the DataGridCell you also lose the border that is actually the control that draws the Background. Add this and it'll work.

<Style TargetType="{x:Type DataGridCell}">
    <Style.Triggers>
        <Trigger Property="IsSelected" Value="True">
            <Setter Property="Background" Value="Red"/>
        </Trigger>
    </Style.Triggers>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type DataGridCell}">
                <Border Background="{TemplateBinding Background}">
                    <ContentPresenter VerticalAlignment="Center"></ContentPresenter>
                </Border>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>
like image 196
Fredrik Hedblad Avatar answered Oct 07 '22 17:10

Fredrik Hedblad