2010-09-23 29 views
5

我定义自定义查找资源字典Button控件:WPF - 在字典中定义与父控件定义的样式混合款式

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> 
    <Style TargetType="Button" x:Key="BaseButtonStyle"> 
    <Setter Property="Background" Value="Blue"/> 
    </Style> 
</ResourceDictionary> 

然后我试图改变风格在窗口中的按钮的位置。

<Window.Resources> 
    <ResourceDictionary> 
    <ResourceDictionary.MergedDictionaries> 
     <ResourceDictionary Source="Dictionary.xaml"/> 
     <ResourceDictionary> 
     <Style TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> 
      <Setter Property="Foreground" Value="Red"/> 
     </Style> 
     </ResourceDictionary> 
     </ResourceDictionary.MergedDictionaries> 
    </ResourceDictionary> 
</Window.Resources> 

在WPF设计师我有我的预期。与红色文本的蓝色按钮。 但是在运行时,两种样式都未应用,并且按钮具有默认颜色。 我该如何解决这个问题?

+0

你能分享xaml吗? – 2010-09-23 13:49:54

回答

6

下面的工作。我只是将样式从MergedDictionaries中移出,并将其放在外部ResourceDictionary上。

<Window.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="Dictionary.xaml"/> 
     </ResourceDictionary.MergedDictionaries> 

     <Style TargetType="Button" BasedOn="{StaticResource BaseButtonStyle}"> 
      <Setter Property="Foreground" Value="Red"/> 
     </Style> 
    </ResourceDictionary> 
</Window.Resources> 

在您的原始XAML中,我不确定为什么设计器能够在WPF运行时没有正确渲染它。 MSDN documentation表示:

合并的ResourceDictionary没有在标记中定义的资源元素。相反,合并的字典是ResourceDictionary,没有定义标记子元素(或者没有通过代码添加元素),但是为Source指定了URI。

它可能与它有关。