如何将样式设置为WPFToolkit DataGrid? [英] How to set Style to WPFToolkit DataGrid ?
问题描述
我在窗口中添加了WPFToolkit的DataGrid.我不知道如何将样式设置为列标题和行?行是动态添加的.
Hi,
I have a WPFToolkit''s DataGrid added to my window. I am not able to figure out how to set Style to the Column Header and Rows ? Rows are added dynamically.
<my:datagrid name="myGrid" xmlns:my="clr-namespace:Microsoft.Windows.Controls;assembly=WPFToolkit" itemssource="{Binding }" autogeneratecolumns="False">
SelectionMode="Extended" SelectionUnit="FullRow" CanUserReorderColumns="False"
ColumnHeaderHeight="42" Background="#FFF7F7F7" BorderBrush="Transparent"
HorizontalGridLinesBrush="#FFEAEAEA" VerticalGridLinesBrush="#FFEAEAEA"
HeadersVisibility="Column" RowHeaderWidth="0" HorizontalContentAlignment="Center"
VerticalContentAlignment="Center" ClipboardCopyMode="None" MinRowHeight="28"
Rowremoved="#FFF7F7F7" RowDetailsVisibilityMode="Visible" RowHeight="28"
DataContextChanged="serverGrid_DataContextChanged">
<my:datagrid.columns>
<my:datagridtextcolumn header="Enabled" width="120" binding="{Binding Path=Name}" />
<my:datagridtextcolumn header="Enabled" width="70" binding="{Binding Path=Country}" />
<my:datagridtextcolumn header="Enabled" width="100" binding="{Binding Path=Description}" />
</my:datagrid.columns>
</my:datagrid>
In the Resources have added code for Style :
<!-- DataGridColumnHeader-->
<Style x:Key="ColumnHeaderStyle" TargetType="{x:Type Thumb}">
<setter property="Background" value="#9DCFD0" />
<setter property="FontFamily" value="Arial Rounded MT" />
<setter property="FontSize" value="14" />
<setter property="FontWeight" value="Bold" />
<setter property="Foreground" value="#00545B" />
<setter property="VerticalContentAlignment" value="Center" />
<setter property="HorizontalContentAlignment" value="Center" />
</Style>
在Style的 TargetType 中,不能将其设置为my:DataGridColumnHeader或仅设置为DataGridColumnHeader.它说"...未找到".在 my:DataGridTextColumn 中,我猜 HeaderStyle 是设置样式的属性.但是能够定义相同的样式.
还有如何设置动态添加的行的样式?
我们非常感谢您的帮助.
In TargetType of Style, am not able to set as my:DataGridColumnHeader or just DataGridColumnHeader. It says "... not found". In my:DataGridTextColumn I guess HeaderStyle is the property to set the style. But am able to define Style for the same.
Also how to set style for Rows added dynamically ?
Any help is highly appreciated.
推荐答案
首先,您应该在包含数据网格的元素中定义名称空间,而不是在数据网格本身中定义.我总是在Window/UserControl声明中声明名称空间.然后,您应该可以在样式声明中将DataGridColumnHeader视为类型.然后在您的Columns声明中,您需要使用StaticResource声明将样式显式设置为正确的资源,如下所示:-
First of all you should define your namespace in an element that contains your datagrid, and not in the datagrid itself. I always declare namespaces in the Window/UserControl declaration. Then you should be able to see DataGridColumnHeader as a Type in your style declaration. Then in you Columns declarations you need to explicitly set the style to the correct resource by using the StaticResource declaration like this:-
<DataGridTextColumn Header="Enabled" Width="120" Binding="{Binding Path=Name}" HeaderStyle="{StaticResource ColumnHeaderStyle}"/>
.
设置行样式是同一回事.使用Key声明资源中的样式,并在数据网格声明中将行样式专门设置为该样式,如下所示:-
.
Styling the Rows is the same thing. Declare the style in your resources with a Key, and specifically set the row style to that style in your datagrid declaration like this:-
<Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
<!--Setters go here-->
</Style>
<...ClipboardCopyMode="None" MinRowHeight="28"
RowDetailsVisibilityMode="Visible" RowHeight="28"
DataContextChanged="serverGrid_DataContextChanged" RowStyle="{StaticResource RowStyle}"
希望对您有帮助
Hope this helps
这篇关于如何将样式设置为WPFToolkit DataGrid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!