我目前正面臨一個非常奇怪的情況。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。
這是預期的行爲嗎?難道我做錯了什麼?
非常感謝您的寶貴時間。
你嘗試用'DynamicResource'而不是'StaticResource'嗎? – Safe
如果你的意思是''Style TargetType =「{x:Type DataGrid}」BasedOn =「{DynamicResource MaterialDesignDataGrid}」>',是的,我做了,但它不起作用。 – Brutus