设置动态datagridtextcolumn标题

发布于 2025-01-24 09:16:32 字数 3473 浏览 2 评论 0 原文

我正在尝试为我的datagrid设置动态标头样式。

我可以为整个DataGrid设置Columnheader样式,但是为单列设置样式不起作用。该样式不应用于列。

mainwindow.xaml

<Window x:Class="StyleTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <CheckBox Content="Themes" IsChecked="False" Unchecked="CheckBox_Unchecked" Checked="CheckBox_Checked" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="60,92,0,0"/>
        <DataGrid d:ItemsSource="{d:SampleData ItemCount=5}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,150,0,0" AutoGenerateColumns="False"
            ColumnHeaderStyle="{DynamicResource ColumnHeaderTest}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="SampleInt" Binding="{Binding SampleInt}"/>
                <DataGridTextColumn Header="SampleStringA" Binding="{Binding SampleStringA}"/>
                <DataGridTextColumn Header="SampleStringB" Binding="{Binding SampleStringB}" HeaderStyle="{DynamicResource ColumnHeaderTest}"/>
                <DataGridCheckBoxColumn Header="SampleBool" Binding="{Binding SampleBool, Mode=OneWay}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

mainwindow.xaml.cs

using System;
using System.Linq;
using System.Windows;

namespace StyleTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (var dictionary in App.Current.Resources.MergedDictionaries.ToList())
            {
                App.Current.Resources.MergedDictionaries.Remove(dictionary);
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            foreach (var dictionary in App.Current.Resources.MergedDictionaries.ToList())
            {
                App.Current.Resources.MergedDictionaries.Remove(dictionary);
            }
            App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Themes/Theme1.xaml") });
        }
    }
}

/theme/them1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type DataGridColumnHeader}" x:Key="ColumnHeaderTest">
        <Setter Property="Background" Value="Green"/>
    </Style>
</ResourceDictionary>

app.xaml和app.xaml.cs均为默认情况下,没有添加代码。

结果

这是一个完整的示例项目,证明了问题。

I am trying to set a dynamic header style for my DataGrid.

I can set the ColumnHeader style for the entire Datagrid, but setting the style for a single column does not work. The style is not applied to the column.

MainWindow.xaml

<Window x:Class="StyleTest.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Grid>
        <CheckBox Content="Themes" IsChecked="False" Unchecked="CheckBox_Unchecked" Checked="CheckBox_Checked" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="60,92,0,0"/>
        <DataGrid d:ItemsSource="{d:SampleData ItemCount=5}" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="0,150,0,0" AutoGenerateColumns="False"
            ColumnHeaderStyle="{DynamicResource ColumnHeaderTest}">
            <DataGrid.Columns>
                <DataGridTextColumn Header="SampleInt" Binding="{Binding SampleInt}"/>
                <DataGridTextColumn Header="SampleStringA" Binding="{Binding SampleStringA}"/>
                <DataGridTextColumn Header="SampleStringB" Binding="{Binding SampleStringB}" HeaderStyle="{DynamicResource ColumnHeaderTest}"/>
                <DataGridCheckBoxColumn Header="SampleBool" Binding="{Binding SampleBool, Mode=OneWay}"/>
            </DataGrid.Columns>
        </DataGrid>
    </Grid>
</Window>

MainWindow.xaml.cs

using System;
using System.Linq;
using System.Windows;

namespace StyleTest
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
        {
            foreach (var dictionary in App.Current.Resources.MergedDictionaries.ToList())
            {
                App.Current.Resources.MergedDictionaries.Remove(dictionary);
            }
        }

        private void CheckBox_Checked(object sender, RoutedEventArgs e)
        {
            foreach (var dictionary in App.Current.Resources.MergedDictionaries.ToList())
            {
                App.Current.Resources.MergedDictionaries.Remove(dictionary);
            }
            App.Current.Resources.MergedDictionaries.Add(new ResourceDictionary() { Source = new Uri("pack://application:,,,/Themes/Theme1.xaml") });
        }
    }
}

/Theme/Them1.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="{x:Type DataGridColumnHeader}" x:Key="ColumnHeaderTest">
        <Setter Property="Background" Value="Green"/>
    </Style>
</ResourceDictionary>

Both App.xaml, and App.xaml.cs are default with no code added.

Result

enter image description here

Here is a complete sample project demonstrating the problem.

https://www.dropbox.com/s/1t2n43gi1umkim7/StyleTest.zip?raw=1

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

执手闯天涯 2025-01-31 09:16:32

我唯一能够提出的解决方案是,每当主题更改时,都要在代码中手动更改标题样式。

private void ThemeChanged()
{
    //a dirty hack to workaround dynamic headerstyle not working
    var CustomHeaderStyle = Application.Current.TryFindResource("CustomHeaderStyle") as Style;
    MyDataGridColumn1.HeaderStyle = CustomHeaderStyle;
}

The only solution I was able to come up with was to manually change the header style in code whenever the theme changes.

private void ThemeChanged()
{
    //a dirty hack to workaround dynamic headerstyle not working
    var CustomHeaderStyle = Application.Current.TryFindResource("CustomHeaderStyle") as Style;
    MyDataGridColumn1.HeaderStyle = CustomHeaderStyle;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文