如何将样式设置为WPFToolkit DataGrid?

发布于 2024-12-03 11:46:24 字数 2252 浏览 0 评论 0原文

我是 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心房的律动 2024-12-10 11:46:24

你可以这样做:

首先是标题的命名空间:

xmlns:Custom="http://schemas.microsoft.com/wpf/2008/toolkit"

然后这是样式:

<Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type Custom:DataGridColumnHeader}">
        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
        <Setter Property="Foreground" Value="#654b24"></Setter>
        <Setter Property="FontWeight" Value="bold"></Setter>
        <Setter Property="Height" Value="30"></Setter>
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#f7f3de" Offset="0.1"/>
                    <GradientStop Color="#Fcfcfc" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>

you can do like this:

First is the namespace to header:

xmlns:Custom="http://schemas.microsoft.com/wpf/2008/toolkit"

then this is style:

<Style x:Key="DataGridColumnHeaderStyle" TargetType="{x:Type Custom:DataGridColumnHeader}">
        <Setter Property="HorizontalAlignment" Value="Center"></Setter>
        <Setter Property="Foreground" Value="#654b24"></Setter>
        <Setter Property="FontWeight" Value="bold"></Setter>
        <Setter Property="Height" Value="30"></Setter>
        <Setter Property="Background">
            <Setter.Value>
                <LinearGradientBrush EndPoint="0.5,1" StartPoint="0.5,0">
                    <GradientStop Color="#f7f3de" Offset="0.1"/>
                    <GradientStop Color="#Fcfcfc" Offset="1"/>
                </LinearGradientBrush>
            </Setter.Value>
        </Setter>
    </Style>
未蓝澄海的烟 2024-12-10 11:46:24

您可以按照这些教程,有四个总计并涵盖 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.

旧人九事 2024-12-10 11:46:24

您看不到 my:DataGridColumnHeader 的可能原因是它位于 System.Windows.Controls.Primitives 命名空间中。 my 也代表这个命名空间吗?

代替使用

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

编辑

然后将 DataGridColumnHeader 引用为“

 <Style TargetType="{x:Type Primitives:DataGridColumnHeader}">
     ....
 </Style>

记住不要为其提供任何键”,以便这适用于所有标题。

Probable reason why you dont see my:DataGridColumnHeader is because its in the System.Windows.Controls.Primitives namespace. Does my represent this namespace as well?

Use

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

instead.

EDIT

Then refer the DataGridColumnHeader as

 <Style TargetType="{x:Type Primitives:DataGridColumnHeader}">
     ....
 </Style>

Remember to NOT give any Key to it so that would apply to all headers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文