You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于ModernWPF主题的CustomDataGrid样式恢复方案咨询

问题

我使用C# WPF搭配ModernWPF主题开发,因需要多选功能,基于官方实现编写了CustomDataGrid。功能可以正常运行,但CustomDataGrid显示为经典旧样式,请问如何让它重新应用ModernWPF的样式?


最小演示项目

MainWindow.xaml

<Grid>
    <local:CustomDataGrid 
        AutoGenerateColumns="True" 
        ItemsSource="{Binding Data}" />
</Grid>

ViewModel.cs

public partial class ViewModel : ObservableObject
{
    [ObservableProperty]
    public DataTable _data;

    public ViewModel()
    {
        var currentData = new DataTable();
        currentData.Columns.Add("Name", typeof(string));
        currentData.Columns.Add("BooleanValue", typeof(bool));

        // 添加两行数据
        DataRow firstRow = currentData.NewRow();
        DataRow secondRow = currentData.NewRow();

        firstRow[0] = "Text1";
        firstRow[1] = true;
        
        secondRow[0] = "Text2";
        secondRow[1] = false;

        currentData.Rows.Add(firstRow);
        currentData.Rows.Add(secondRow);

        _data = currentData;
    }
}

CustomDataGrid.cs

public class CustomDataGrid : DataGrid
{
    public CustomDataGrid()
        => SelectionChanged += CustomDataGrid_SelectionChanged;

    void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        => SelectedItemsList = SelectedItems;

    #region SelectedItemsList

    public IList SelectedItemsList
    {
        get => (IList)GetValue(SelectedItemsListProperty);
        set => SetValue(SelectedItemsListProperty, value);
    }

    public static readonly DependencyProperty SelectedItemsListProperty =
            DependencyProperty.Register("SelectedItemsList", typeof(IList), typeof(CustomDataGrid), new PropertyMetadata(propertyChangedCallback: null));

    #endregion
}

解决方法

方法1:在XAML中显式指定样式

在使用CustomDataGrid时,直接引用ModernWPF为DataGrid定义的默认样式:

<local:CustomDataGrid 
    AutoGenerateColumns="True" 
    ItemsSource="{Binding Data}"
    Style="{StaticResource {x:Type DataGrid}}" />

方法2:在CustomDataGrid中重写默认样式键(推荐)

在CustomDataGrid的静态构造函数中,将默认样式键设置为基类DataGrid的样式键,这样所有CustomDataGrid实例都会自动继承ModernWPF的样式,无需逐个指定:

public class CustomDataGrid : DataGrid
{
    // 添加静态构造函数重写样式键
    static CustomDataGrid()
    {
        DefaultStyleKeyProperty.OverrideMetadata(typeof(CustomDataGrid), 
            new FrameworkPropertyMetadata(typeof(DataGrid)));
    }

    public CustomDataGrid()
        => SelectionChanged += CustomDataGrid_SelectionChanged;

    void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
        => SelectedItemsList = SelectedItems;

    #region SelectedItemsList

    public IList SelectedItemsList
    {
        get => (IList)GetValue(SelectedItemsListProperty);
        set => SetValue(SelectedItemsListProperty, value);
    }

    public static readonly DependencyProperty SelectedItemsListProperty =
            DependencyProperty.Register("SelectedItemsList", typeof(IList), typeof(CustomDataGrid), new PropertyMetadata(propertyChangedCallback: null));

    #endregion
}

内容的提问来源于stack exchange,提问作者Enki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.23 19:35:38