我有以下的自定義控制沒有找到命名風格Generic.xaml
public class MagicButton : Control
{
static MagicButton()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(MagicButton),
new FrameworkPropertyMetadata(typeof(MagicButton)));
}
}
有以下主題/ Generic.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:WpfApplication4">
<Style TargetType="{x:Type l:MagicButton}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:MagicButton}">
<Border />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<Style TargetType="{x:Type l:MagicButton}"
x:Key="OtherStyle">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type l:MagicButton}">
<Border />
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
當我使用使用MagicButton控制默認樣式,一切正常,但是當我嘗試使用「OtherStyle」時
<l:MagicButton Style="{StaticResource OtherStyle}"/>
它找不到具有例外「{」的樣式找不到名爲'OtherStyle'的資源。資源名稱區分大小寫。「}」
但是,如果我將OtherStyle移動到App.xaml,它就可以工作。
我不想把它的App.xaml不過,我想是我的主題/ Generic.xaml文件包含默認樣式,以及命名鍵樣式,人們可以明確地選擇加入到使用
檢查你的代碼,我有測試,沒有錯誤。 ' –
Rang
'Themes/Generic.xaml'與試圖使用'OtherStyle'的代碼位於同一個項目中。爲什麼我必須顯式添加ResourceDictionary?它似乎沒有這樣做的默認風格。 – wforl