2017-04-07 178 views
1

我目前正面临一个非常奇怪的情况。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,我想默认应用于我的应用程序的所有DataGrid(在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> 

数据网格样式不适用出于某种原因,但它的作品,如果我把它直接内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。

这是预期的行为吗?难道我做错了什么?

非常感谢您的宝贵时间。

+0

你尝试用'DynamicResource'而不是'StaticResource'吗? – Safe

+0

如果你的意思是''Style TargetType =“{x:Type DataGrid}”BasedOn =“{DynamicResource MaterialDesignDataGrid}”>',是的,我做了,但它不起作用。 – Brutus

回答

0

为DataGridStyles.xaml的构建操作尝试不同的设置。我认为它应该工作,如果你把它设置为页。

+0

它实际上被设置为“页面”但不工作。 – Brutus

+0

Connell,它可能是与ResourceDictionaries合并在一起的顺序有关的问题。 – Brutus