WPat datagrid style

I want the WPat datagrid style to be very simple. As far as I understand, I should have a code, for example:

<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 how to tell datagrid how to use this style above?

Regards, S

+4
source share
5 answers

Put it in xaml resource (local or global). The easiest way is to put it in a 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> 
+2
source

The best place for styles is the resource dictionary referenced by the App.xaml application.

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> 

Link to 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 also work for headers):

 <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 the target type of TextBlock.

+1
source

Regarding "The type DataGridColumnHeader was not found": you need a second xml namespace entry because 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 a link to a new namespace in your style, for example

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

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 it is not included in the UserControl, you can use the "Window" or any other container in which you are located.

You also need to reference it in your datagrid with:

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

The following code does not seem to work for me. The error message I get is: "Type DataGridColumnHeader not found.".

 <UserControl x:Class="myprogram.InfoView" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" Height="500" Width="600" Loaded="UserControl_Loaded"> <UserControl.Resources> <Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type my:DataGridColumnHeader}" > <Setter Property="Background" Value="#88800080" /> <Setter Property="Foreground" Value="White" /> </Style> </UserControl.Resources> <DockPanel LastChildFill="True"> ........ </DockPanel> </UserControl> 
0
source

Source: https://habr.com/ru/post/1301486/


All Articles