如何将样式写入 WPF DataGridColumnHeader

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18763721/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 09:38:08  来源:igfitidea点击:

How to write style to WPF DataGridColumnHeader

c#csswpfxamldatagrid

提问by user2632652

I want to write style for WPF DataGrid Column Header . My grid as follows

我想为 WPF DataGrid Column Header 编写样式。我的网格如下

`<DataGrid >
    <DataGrid.Columns>
        <DataGridTemplateColumn>
              <DataGridTemplateColumn.HeaderTemplate>
                 <DataTemplate>
                      <Label Content="{DynamicResource colName}"></Label>
                 </DataTemplate>
              </DataGridTemplateColumn.HeaderTemplate>
         </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>`

I want to bind label content using DynamicResource. This code is working properly. I want write a style to apply this binding method to grid column. I wrilte a style as follows.

我想使用 DynamicResource 绑定标签内容。此代码工作正常。我想编写一个样式来将此绑定方法应用于网格列。我写了一个样式如下。

`<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type DataGridColumnHeader}" > 
     <Setter Property="Template">
            <Setter.Value>
                  <ControlTemplate TargetType="DataGridColumnHeader">
                           <Label Content="{ TemplateBinding Content}"/>
                   </ControlTemplate>
             </Setter.Value>
       </Setter>
</Style>`

I apply this style to grid as follows.

我将这种样式应用于网格如下。

<DataGrid >
     <DataGrid.Columns>
          <DataGridTextColumn  HeaderStyle="{StaticResource ColumnHeaderStyle}" Header="{ DynamicResource colName}"  />
     </DataGrid.Columns>
</DataGrid>

After using style header not binding. How can I solve this problem?

使用样式头后不绑定。我怎么解决这个问题?

Thanks!

谢谢!

回答by Debashrita

We can try as follows

我们可以尝试如下

<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type dg:DataGridColumnHeader}">
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="Background" Value="Black" />
    <Setter Property="Foreground" Value="White" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="dg:DataGridColumnHeader">   
                <dg:DataGridHeaderBorder
                x:Name="headerBorder"                      
                Background="Red">
                <Border BorderThickness="1" 
                        CornerRadius="2" 
                        Background="Black"
                        BorderBrush="Green">                            
                    <Grid>
                        <TextBlock Text="{TemplateBinding  Content}" 
                                   VerticalAlignment="Center" 
                                   HorizontalAlignment="Center" 
                                   TextWrapping="Wrap"/>                                
                        </Grid>                                
                </Border>
                </dg:DataGridHeaderBorder>

            </ControlTemplate>
        </Setter.Value>
    </Setter>                 
</Style>
<dg:DataGrid Grid.Row="1" Grid.RowSpan="1" 
                 Name="UserName"                      
                 HorizontalAlignment="Left"
                 AutoGenerateColumns="True"
                 Width="800"                       
                 Background="Yellow"
                 ColumnHeaderHeight="20"                       
                 ColumnHeaderStyle="{DynamicResource ColumnHeaderStyle}"
                 RowStyle="{StaticResource RowStyle}"
                 CanUserAddRows="False"
                 CanUserDeleteRows="False"
                 />

回答by Nitin

Define Headeras a StaticResourcenot Dynamic. This will fix your issue

定义HeaderStaticResource不是动态的。这将解决您的问题

<DataGridTextColumn  HeaderStyle="{StaticResource ColumnHeaderStyle}" Header="{ StaticResource colName}"  />

Or you can update your controlTemplate's label to have DynamicResourceand no need of giving Headerthen.

或者你可以更新你的 controlTemplate 的标签,然后DynamicResource不需要给出Header

<ControlTemplate TargetType="DataGridColumnHeader">
                        <Label Content="{ DynamicResource colName}"/>
                    </ControlTemplate>