XAML资源字典 - 无法应用默认样式

时间:2017-04-07 12:50:50

标签: c# wpf xaml

我目前面临一个非常奇怪的情况。

我有一个WPF应用程序,其中包含 App.xaml 中的以下行:

<Application.Resources>
    <ResourceDictionary>
        <ResourceDictionary.MergedDictionaries>
            <ResourceDictionary>
                <local:AppBootstrapper x:Key="bootstrapper" />
            </ResourceDictionary>
            <ResourceDictionary Source="./Styles/MyTheme.xaml"/>
        </ResourceDictionary.MergedDictionaries>
    </ResourceDictionary>
</Application.Resources>

然后我在 MyTheme.xaml

中有一些词典
<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="./UserControlStyles.xaml"/>
    <ResourceDictionary Source="./WizardStyle.xaml"/>
    <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Blue.xaml" />

    <ResourceDictionary Source="./DataGridStyles.xaml"/>
    <ResourceDictionary Source="./TreeViewStyles.xaml"/>
    <ResourceDictionary Source="./ToggleButtonStyles.xaml"/>
</ResourceDictionary.MergedDictionaries>

最后,我希望默认情况下将一个DataGrid应用于我的应用程序的所有DataGrids(在 DataGridStyles.xaml 中):

<ResourceDictionary>
        <Style TargetType="{x:Type DataGrid}" BasedOn="{StaticResource MaterialDesignDataGrid}">
            <Setter Property="AutoGenerateColumns" Value="False" />
            <Setter Property="CanUserAddRows" Value="False" />
            <Setter Property="CanUserDeleteRows" Value="False" />
            <Setter Property="SelectionMode" Value="Single" />
        </Style>
    </ResourceDictionary>

由于某种原因,DataGrid样式不适用,但如果我将其直接放在 MyTheme.xaml 中,它就可以了:

<ResourceDictionary.MergedDictionaries>
    <ResourceDictionary Source="./UserControlStyles.xaml"/>
    <ResourceDictionary Source="./WizardStyle.xaml"/>
    <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Dark.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MaterialDesignThemes.Wpf;component/Themes/MaterialDesignTheme.Defaults.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Primary/MaterialDesignColor.Blue.xaml" />
    <ResourceDictionary Source="pack://application:,,,/MaterialDesignColors;component/Themes/Recommended/Accent/MaterialDesignColor.Blue.xaml" />

    <ResourceDictionary Source="./DataGridStyles.xaml"/>
    <ResourceDictionary Source="./TreeViewStyles.xaml"/>
    <ResourceDictionary Source="./ToggleButtonStyles.xaml"/>

    <ResourceDictionary>
        <Style TargetType="{x:Type DataGrid}" BasedOn="{StaticResource MaterialDesignDataGrid}">
            <Setter Property="AutoGenerateColumns" Value="False" />
            <Setter Property="CanUserAddRows" Value="False" />
            <Setter Property="CanUserDeleteRows" Value="False" />
            <Setter Property="SelectionMode" Value="Single" />
        </Style>
    </ResourceDictionary>
</ResourceDictionary.MergedDictionaries>

请注意, MaterialDesignDataGrid 是在MaterialDesignTheme.Defaults.xaml中定义的。

这是预期的行为吗?我做错了吗?

非常感谢你的时间。

1 个答案:

答案 0 :(得分:0)

尝试使用DataGridStyles.xaml的构建操作的不同设置。如果你把它设置为Page,我认为它应该有效。