隐式样式在App.xaml中不起作用,但正在使用本地页面资源。如何为控件创建全局样式?
<navigation:Page.Resources>
<Style TargetType="Button">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Navy" />
</Style>
</navigation:Page.Resources>
答案 0 :(得分: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
希望帮助!
答案 1 :(得分:0)
适用于按钮!
<Style TargetType="ButtonBase">
<Setter Property="Background" Value="Red" />
<Setter Property="Foreground" Value="Navy" />
</Style>