我正在使用Codeplex中的WPF Datagrid。
我可以在dg:DataGrid元素中设置行的样式并使用以下属性。
但我如何设计标题样式?我在网上找到了100个定义样式并使用例如样本的例子。 x:Key =“DataGridColumnHeaderStyle”在Datagrid元素中,但它们似乎都不适用于我。
我怎样才能在此DataGrid上将Datagrid标题背景更改为橙色?
<dg:DataGrid AlternatingRowBackground="#ddd"
RowBackground="#eee"
Name="theGrid1"
VerticalAlignment="Stretch"
AutoGenerateColumns="False"
BorderBrush="#ddd">
...
</dg:DataGrid>
答案 0 :(得分:11)
DataGrid上还有一个属性,允许为标题设置样式:
<DataGrid.ColumnHeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight"
Value="Bold" />
</Style>
</DataGrid.ColumnHeaderStyle>
答案 1 :(得分:2)
在这种情况下,样式位于名为generic.xaml的文件中,它应该在项目的themems文件夹中进行处理。
找到并打开它。在里面你会发现这一行控制列标题的背景 <dg:DataGridHeaderBorder SortDirection="{TemplateBinding SortDirection}"
IsHovered="{TemplateBinding IsMouseOver}"
IsPressed="{TemplateBinding IsPressed}"
IsClickable="{TemplateBinding CanUserSort}"
Background="{TemplateBinding Background}"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
Padding ="{TemplateBinding Padding}"
SeparatorVisibility="{TemplateBinding SeparatorVisibility}"
SeparatorBrush="{TemplateBinding SeparatorBrush}">
基本上是在模板中的另一个地方定义的: 这将向您解释TemlateBinding MSDN TemplateBinding
HTH, 埃里克
答案 2 :(得分:0)
这是另一个样本
<DataGrid AutoGenerateColumns="False" Height="200"
HorizontalAlignment="Left" Name="dgDownloads"
VerticalAlignment="Top" Width="777"
Background="Black" RowBackground="Gray" Foreground="White"
AlternatingRowBackground="Gray" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch">
<DataGrid.Columns>
<DataGridTextColumn
Header="{lex:LocTextExtension Key=Name, Dict=Resources, Assembly=PreShow.Player}"
Width="220"
IsReadOnly="True"
Binding="{Binding Filename}" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="TextWrapping" Value="Wrap"/>
</Style>
</DataGridTextColumn.ElementStyle>
<DataGridTextColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Yellow" />
<Setter Property="Background" Value="Black" />
</Style>
</DataGridTextColumn.HeaderStyle>
</DataGridTextColumn>
<DataGridCheckBoxColumn
IsReadOnly="True"
Header="{lex:LocTextExtension Key=Success, Dict=Resources, Assembly=PreShow.Player}"
Width="60"
Binding="{Binding IsSuccess}"
IsThreeState="False">
<DataGridCheckBoxColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Yellow" />
<Setter Property="Background" Value="Black" />
</Style>
</DataGridCheckBoxColumn.HeaderStyle>
</DataGridCheckBoxColumn>
<DataGridTemplateColumn Header="{lex:LocTextExtension Key=Time, Dict=Resources, Assembly=PreShow.Player}" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Downloaded}" Margin="4"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
<DataGridTemplateColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Yellow" />
<Setter Property="Background" Value="Black" />
</Style>
</DataGridTemplateColumn.HeaderStyle>
</DataGridTemplateColumn>
<DataGridCheckBoxColumn
IsReadOnly="True"
Header="{lex:LocTextExtension Key=IsDownloading, Dict=Resources, Assembly=PreShow.Player}"
Width="60"
Binding="{Binding IsDownloading}"
IsThreeState="False">
<DataGridCheckBoxColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Yellow" />
<Setter Property="Background" Value="Black" />
</Style>
</DataGridCheckBoxColumn.HeaderStyle>
</DataGridCheckBoxColumn>
<DataGridHyperlinkColumn
Header="URL"
Width="Auto"
IsReadOnly="True"
Binding="{Binding Path=URL}"
TargetName="{Binding Path=URL}">
<DataGridHyperlinkColumn.ElementStyle>
<Style TargetType="TextBlock">
<EventSetter Event="Hyperlink.Click" Handler="OnHyperlinkClick" />
</Style>
</DataGridHyperlinkColumn.ElementStyle>
<DataGridHyperlinkColumn.HeaderStyle>
<Style TargetType="{x:Type DataGridColumnHeader}">
<Setter Property="FontWeight" Value="Bold" />
<Setter Property="Foreground" Value="Yellow" />
<Setter Property="Background" Value="Black" />
</Style>
</DataGridHyperlinkColumn.HeaderStyle>
</DataGridHyperlinkColumn>
</DataGrid.Columns>
</DataGrid>