2012-11-03 99 views
1

隐式样式不适用于App.xaml,但使用本地页面资源。我如何为控制制定全球风格?隐式应用程序的样式

<navigation:Page.Resources> 
<Style TargetType="Button"> 
    <Setter Property="Background" Value="Red" /> 
    <Setter Property="Foreground" Value="Navy" /> 
</Style> 
</navigation:Page.Resources> 

回答

0

在App.xaml中

<Application.Resources> 
    <ResourceDictionary> 
     <ResourceDictionary.MergedDictionaries> 
      <ResourceDictionary Source="StilDict.xaml" /> 
     </ResourceDictionary.MergedDictionaries> 
</ResourceDictionary> 

在使用样式您的项目在任何地方StilDict.xaml

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    ,,,, and what you use else> 
<Style x:Key="ButtonStyleNavyOnRed" TargetType="Button"> 
<Setter Property="Background" Value="Red" /> 
<Setter Property="Foreground" Value="Navy" /> 
</Style> 

<UserControl> 
    <Button Style={StaticResource ButtonStyleNavyOnRed} Content="Yahoo! :)"/> 
</UserControl> 

重要说明:如果删除x:Key =“ButtonStyleNavyOnRed”部分,则所有Target类型都会获取此样式,但不会获得Button派生对象。 http://msdn.microsoft.com/en-us/library/system.windows.style(v=vs.95).aspx

希望有助于!

+0

这是明确的。 –

+0

哦,你是对的。只需要删除x:Key =“ButtonStyleNavyOnRed”部分。然后它测试我的工作。感谢您的警告。 –

+0

是的,我正在使用app.xaml中的Merge字典。从我读的时候,当你没有在样式中指定一个键时,它会隐式地将样式设置为声明中TargetType的所有控件。我试图让这个工作在全球范围内工作,而不仅仅是像上面所显示的那样在页面资源内工作。我不知道该怎么改变来解决这个问题。感谢您的建议。 – OneFineDay

0

这适用于按钮!

<Style TargetType="ButtonBase"> 
<Setter Property="Background" Value="Red" /> 
<Setter Property="Foreground" Value="Navy" /> 
</Style> 
+0

它只适用于buttonbase对象,还是适用于按钮? –

+0

它应该是从ButtonBase派生的所有对象。它是奇怪的,TargetType = ButtonBase最初工作,然后它只是退出工作,我切换到TargetType =按钮,它再次工作? – OneFineDay

+0

请按照我帖子的链接末尾MS说:“如果隐式设置样式,则样式仅适用于与TargetType完全匹配的类型,而不适用于从TargetType值派生的元素” –