2010-09-23 30 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。

它可能與它有關。