如何将样式设置为WPFToolkit DataGrid?
我是 WPF 新手,正在使用 C# 和 .NET3.5 进行开发。我已将 WPFToolkit 的 DataGrid 添加到我的窗口中。我无法弄清楚如何将样式设置为列标题和行?行是动态添加的。
<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>
在资源中添加了 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
是设置样式的属性。但我能够为其定义样式。
另外如何为动态添加的行设置样式?我哪里错了? 非常感谢任何帮助。
I am new to WPF, am developing using C# and .NET3.5. 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>
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? Where am I going wrong?
Any help is highly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以这样做:
首先是标题的命名空间:
然后这是样式:
you can do like this:
First is the namespace to header:
then this is style:
您可以按照这些教程,有四个总计并涵盖
DataGrid
样式的大部分方面。链接的文章是该系列的第二篇文章,涵盖了大部分基础知识。
You can follow these tutorials, there are four in total and cover most aspects of
DataGrid
styling.The linked article is the second in the series and covers most of the basics.
您看不到
my:DataGridColumnHeader
的可能原因是它位于System.Windows.Controls.Primitives
命名空间中。my
也代表这个命名空间吗?代替使用
。
编辑
然后将 DataGridColumnHeader 引用为“
记住不要为其提供任何键”,以便这适用于所有标题。
Probable reason why you dont see
my:DataGridColumnHeader
is because its in theSystem.Windows.Controls.Primitives
namespace. Doesmy
represent this namespace as well?Use
instead.
EDIT
Then refer the DataGridColumnHeader as
Remember to NOT give any Key to it so that would apply to all headers.