开发者

WPF datagrid styling

开发者 https://www.devze.com 2022-12-20 14:15 出处:网络
I want to style a WPF datagrid and it seems to be really easy . As far as I understand I have to have code such as the following:

I want to style a WPF datagrid and it seems to be really easy . As far as I understand I have to have code such as the following:

<Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type Custom:DataGridColumnHeader}"  >
<Setter Property="Background" Value="#88800080" />
    <Setter Property="Foreground" Value="White" /> 
</Style>

But my question is ..where do I place this code and ho开发者_开发问答w do I let the datagrid know to use the style above ?

Regards, S


Put it in the resource of the xaml (local or global). The easiest is to put it in the local resource of the current xaml file:

<Page Name="SomeName"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>
    <Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type Custom:DataGridColumnHeader}"  >
      <Setter Property="Background" Value="#88800080" />
      <Setter Property="Foreground" Value="White" /> 
   </Style>
  </Page.Resources>
<!-- The rest of the xaml -->
</Page>


The best place to put styles is in a resource dictionary, referenced in App.xaml.

Resource dictionary ("StyleResources.xaml" in this example):

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style x:Key="TextBlockRightAlign" TargetType="TextBlock">
        <Setter Property="HorizontalAlignment" Value="Right" />
    </Style>
    <Style x:Key="TextBlockTitle" TargetType="TextBlock">
        <Setter Property="FontSize" Value="20" />
        <Setter Property="FontWeight" Value="Bold" />
    </Style>
</ResourceDictionary>

Referencing the style dictionary in App.xaml:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary Source="StyleResources.xaml"/>
        </ResourceDictionary.MergedDictionaries>
        <ValueConverters:PriceConverter x:Key="PriceConverter"/>
    </ResourceDictionary>
</Application.Resources>

Using the definition in a datagrid (column formatting here, but should work for headers as well):

<data:DataGridTextColumn Header="Charge" Width="100" 
       Binding="{Binding Charge, Mode=TwoWay, Converter={StaticResource PriceConverter}}"
       ElementStyle="{StaticResource TextBlockRightAlign}" />

Note that the element inside the cell is a TextBlock, so you can use a style with a target type of TextBlock.


As for the "Type DataGridColumnHeader was not found": you need a second xml namespace entry since the DataGridColumnHeader is in the System.Windows.Controls.Primitives namespace. You need something like

xmlns:dg="clr-namespace:Microsoft.Windows.Controls.Primitives;assembly=WPFToolkit"

and then reference the new namespace in your style, e.g.

<Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type dg:DataGridColumnHeader}" >


Styles usually go:

<UserControl.Resources>
    <Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type Custom:DataGridColumnHeader}"  >
       <Setter Property="Background" Value="#88800080" />
       <Setter Property="Foreground" Value="White" /> 
    </Style>
</UserControl.Resources>

Use the appropriate container if this isn't within a UserControl you may use "Window" or whatever container you're in.

Also you need to reference it in your datagrid with:

<Custom:DataGrid ColumnHeaderStyle="{StaticResource DataGridColumnHeaderStyle}"/>
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号