Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
SQL
I want to set a style for my DataGrid, but I do not know where is the problem
the backgroud property does not work with its value in the presence of the Template property.


my code:
XML
<Window.Resources>

        <Style x:Key="DataGridStyle1" TargetType="{x:Type DataGrid}">
            <Setter Property="CellStyle" Value="{DynamicResource GridStyle1}"/>
        </Style>



XML
<Style x:Key="GridStyle1" TargetType="DataGridCell">
            <Style.Triggers>
                <Trigger Property="IsSelected" Value="True" >
                    <Setter Property="Background" Value="SeaGreen"/>
                </Trigger>
            </Style.Triggers>
            <Style.Setters>
            <Setter Property="Template">

XML
<Setter.Value>
                    <ControlTemplate TargetType="{x:Type DataGridCell}">
                        <Border Name="DataGridCellBorder">
                            <ContentControl Content="{TemplateBinding Content}">
                                <ContentControl.ContentTemplate>

<DataTemplate>
<TextBlock Background="Transparentquot; TextWrapping="WrapWithOverflow" TextTrimming="CharacterEllipsis" Height="auto" Width="auto" Text="{Binding Text}"/>
</DataTemplate>
</ContentControl.ContentTemplate>
</ContentControl>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
XML
</Window.Resources>


<DataGrid Style="{DynamicResource DataGridStyle1}" AutoGenerateColumns="True" SelectionChanged="grid_Pr_SelectionChanged"/>

Help me please.


</Style.Setters>
</Style>
Posted

1 solution

I cannot tell you the reasons it works the way it does, but I can help you fix what is wrong. Essentially, your template is overriding your cell style.

I bet if you remove the whole template section it'll work. But, assuming you want the behavior, you'll have to add it within the control template. The binding is a little more complicated.
<DataTemplate>
    <TextBlock TextWrapping="WrapWithOverflow"
                TextTrimming="CharacterEllipsis" Height="Auto" Width="Auto"
                Text="{Binding Text}">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding IsSelected, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type DataGridCell}}}" Value="True">
                        <Setter Property="Background" Value="SeaGreen" />
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</DataTemplate>


P.S. You should be using StaticResource not DynamicResource. You don't need it here, and you're incurring a little more overhead because of it.
 
Share this answer
 
v3
Comments
nasrsoft 2-Mar-14 23:37pm    
thanks it works..
tgrt 3-Mar-14 8:07am    
Glad to hear it!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900