【问题标题】:How to Set style for DataGrid header in WPF如何在 WPF 中为 DataGrid 标题设置样式
【发布时间】:2011-12-18 03:03:46
【问题描述】:

我有一个这样的DataGrid

<DataGrid AutoGenerateColumns="False"  
          Height="221" 
          HorizontalAlignment="Center" 
          VerticalContentAlignment="Center" 
          HorizontalContentAlignment="Center" 
          Margin="6,269,0,0" 
          Name="dataGrid1" 
          VerticalAlignment="Center" 
          Width="875" 
          SelectionChanged="dataGrid1_SelectionChanged">
    <DataGrid.Columns>
        <DataGridTextColumn x:Name="Id" 
                            Header="ID"
                            Binding="{Binding Path=Key}" 
                            HeaderStyle="" />
        <DataGridTemplateColumn Header="Image" 
                                Width="SizeToCells" 
                                IsReadOnly="True" 
                                MinWidth="80">
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <Image Width="16" 
                           Height="16"
                           Source="{StaticResource MyImageSource}"
                           HorizontalAlignment="Center" 
                           VerticalAlignment="Center"/>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

我怎样才能使DataGrid Header居中?并为其应用样式?

谢谢


编辑 1):

写完这段代码后:

<DataGrid.ColumnHeaderStyle>
    <Style TargetType="{x:Type DataGridColumnHeader}">
        <Setter Property="FontWeight"  Value="Bold" />
        <Setter Property="HorizontalAlignment" Value="Center" />
        <Setter Property="HorizontalContentAlignment" Value="Center" />
    </Style>
</DataGrid.ColumnHeaderStyle>

网格列变为:

为什么?

【问题讨论】:

    标签: c# wpf c#-4.0 styles


    【解决方案1】:

    为避免标题折叠的影响,只需以这种方式更正您的样式:

      <Setter Property="HorizontalAlignment" Value="Stretch" />
      <Setter Property="HorizontalContentAlignment" Value="Center" />
    

    【讨论】:

    • 不需要 因为“Stretch”是默认值。
    【解决方案2】:

    为Datagrid的DatagridColumnHeader添加样式

    <DataGrid Name="GatewayPortsDataGrid" Height="auto" Width="auto">
         <DataGrid.Resources>
                <Style BasedOn="{StaticResource {x:Type DataGridColumnHeader}}" TargetType="{x:Type DataGridColumnHeader}" >
                <Setter Property="Background" Value="White" />
                <Setter Property="Foreground" Value="Black" />                                   
                <Setter Property="BorderBrush" Value="Black"/>                                  
                <Setter Property="BorderThickness" Value="1 1 1 1"/>                               
                <Setter Property="Margin" Value="-1,-1,0,0" />
                <Setter Property="Height" Value="28" />                                                                    
                <Setter Property="Width" Value="auto"/>                                    
                <Setter Property="HorizontalContentAlignment" Value="Center"/>                                                                        
                </Style>
            </DataGrid.Resources>
    </DataGrid>
    

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 2014-06-13
      • 1970-01-01
      • 2012-02-02
      • 1970-01-01
      • 2011-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多