Pregunta

I have tried an inline style which works fine.

<DataGrid AutoGenerateColumns="False" AlternatingRowBackground="{x:Null}" DataContext="{StaticResource UserGroups}" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Name}"
                    <DataGridTextColumn.HeaderStyle>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="HorizontalContentAlignment" Value="Center"/>
                <Setter Property="VerticalContentAlignment" Value="Bottom"/>
            </Style>
            </DataGridTextColumn.HeaderStyle>
        </DataGridTextColumn>
    </DataGrid.Columns>
</DataGrid>

but I would like the style in the resources, so I tried this which does not work.

<UserControl.Resources>

    <DataTemplate x:Key="headerTemplate">
        <TextBlock HorizontalAlignment="Center" VerticalAlignment="Bottom"  Text="{Binding}"/>
    </DataTemplate>

    <Style x:Key="dgColumnHeaderStyle" TargetType="DataGridColumnHeader">
        <Setter Property="HorizontalContentAlignment" Value="Center"/>
        <Setter Property="VerticalContentAlignment" Value="Bottom"/>
    </Style>
</UserControl.Resources>

<DataGrid AutoGenerateColumns="False" AlternatingRowBackground="{x:Null}" DataContext="{StaticResource UserGroups}" ItemsSource="{Binding}">
    <DataGrid.Columns>
        <DataGridTextColumn Header="Name" HeaderStyle="dgColumnHeaderStyle" HeaderTemplate="{StaticResource headerTemplate}" Binding="{Binding Path=Name}"/>
    </DataGrid.Columns>
</DataGrid>

Probably something simple but I've only just started using this control. How do I fix the style in the second example so it works?

¿Fue útil?

Solución

To access Resource you have to use the StaticResource or DynamicResource key words

Change:

HeaderStyle="dgColumnHeaderStyle"

To:

HeaderStyle="{StaticResource dgColumnHeaderStyle}"
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top