Search code examples
c#wpfvisual-studio-2013wpfdatagriddatagridcolumnheader

Member "BackGround" is not recognized or is not accessible


I'm relative new to WPF and I have check on several tutorials on how to style a DataGrid. All of them use the same examples but when I try to implement them into my project, came this annoying message.

This is the code I've been trying to implement, I got this from MSDN page:

<Window.Resources>
<!-- DataGrid style -->
<Style x:Key="DataGridStyle1" TargetType="{x:Type DataGrid}">
    <Setter Property="ColumnHeaderStyle" Value="{DynamicResource ColumnHeaderStyle1}"/>
</Style>
<!-- DataGridColumnHeader style -->
<Style x:Key="ColumnHeaderStyle1" TargetType="DataGridColumnHeader">
    <Setter Property="Height" Value="30"/>
    <Setter Property="Background" Value="LightBlue"/>
    <Setter Property="Foreground" Value="Blue"/>
    <Setter Property="FontSize" Value="18" />
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="ToolTip" Value="Click to sort."/>
        </Trigger>
    </Style.Triggers>
</Style>

I'm using: - C# Framework 4.5.1 - Blend for Visual Studio 2013 - Visual Studio 2013 (I got the error in both places).

All the imports and references are just fine.

I have check the solution properties and my Platform Target is "Any CPU"

My guess is this property might be deprecated for this control (DataGridColumnHeader).

Hope anyone could tell the proper way to acheive my goal.

Thanks in advance


Solution

  • It's trying to use a DynamicResource before it's defined.. Try swapping the Styles. Also, there's really no need for a DynamicResource here IMO, just change it to StaticResource.

    <Window.Resources>
        <!-- DataGridColumnHeader style -->
        <Style x:Key="ColumnHeaderStyle1" TargetType="DataGridColumnHeader">
            <Setter Property="Height" Value="30"/>
            <Setter Property="Background" Value="LightBlue"/>
            <Setter Property="Foreground" Value="Blue"/>
            <Setter Property="FontSize" Value="18" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="ToolTip" Value="Click to sort."/>
                </Trigger>
            </Style.Triggers>
        </Style>
        <!-- DataGrid style -->
        <Style x:Key="DataGridStyle1" TargetType="{x:Type DataGrid}">
            <Setter Property="ColumnHeaderStyle" Value="{StaticResource ColumnHeaderStyle1}"/>
        </Style>
    </Window.Resources>