小编Bur*_*ger的帖子

WPF没有应用MergedDictionaries中定义的默认样式?

在WPF应用程序中,我在单独的资源字典中定义了默认控件样式(例如"ButtonStyle.xaml"),并将它们作为合并字典添加到名为"ResDictionary.xaml"的资源字典中.

如果我在我的App.xaml中将此"ResDictionary.xaml"称为合并字典,则不会应用默认样式.但是,如果我引用"ButtonStyle.xaml",它可以正常工作.

如果我在.NET 3.5或3.0中重新编译相同的代码,它会识别并应用"App.xaml"到"ResDictionary.xaml"中引用的默认样式,但不能在.NET 4.0中应用.

在运行时,如果我检查Application.Current.Resources字典,那么默认样式就在那里,但只有在Button控件中显式指定Style属性时才会应用它们.

是否有任何解决方案在.NET 4.0中以这种方式引用资源字典(包含默认样式)?


App.xaml中:

<Application.Resources>
  <ResourceDictionary>
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="Styles/ResDictionary.xaml"/>
    </ResourceDictionary.MergedDictionaries>
  </ResourceDictionary>
</Application.Resources>
Run Code Online (Sandbox Code Playgroud)

ResDictionary.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="Default/ButtonStyle.xaml"/>
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

ButtonStyle.xaml:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="Button">
        <Setter Property="Background" Value="Yellow"/>
    </Style>
</ResourceDictionary>
Run Code Online (Sandbox Code Playgroud)

c# wpf .net-4.0

29
推荐指数
2
解决办法
1万
查看次数

标签 统计

.net-4.0 ×1

c# ×1

wpf ×1