代码之家  ›  专栏  ›  技术社区  ›  devuxer

如何在不将WPF从Aero恢复为Classic的情况下更改按钮的默认样式?

  •  6
  • devuxer  · 技术社区  · 15 年前

    <Application
        x:Class="TestApp.App"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
        <Application.Resources>
            <ResourceDictionary>
                <ResourceDictionary.MergedDictionaries>
                    <ResourceDictionary
                        Source="/PresentationFramework.Aero;component/themes/Aero.NormalColor.xaml" />
                    <ResourceDictionary
                         Source="pack://application:,,,/WPFToolkit;component/Themes/Aero.NormalColor.xaml" />
                    <ResourceDictionary
                        Source="/CommonLibraryWpf;component/ResourceDictionaries/ButtonResourceDictionary.xaml" />
                        <!-- Note, ButtonResourceDictionary.xaml is defined in an external class library-->
                </ResourceDictionary.MergedDictionaries>
            </ResourceDictionary>
        </Application.Resources>
    </Application>
    

    <Style TargetType="Button">
        <Setter Property="Padding" Value="3" />
        <Setter Property="FontWeight" Value="Bold" />
    </Style>
    

    所有按钮现在都有正确的填充和粗体文本,但它们看起来“经典”,而不是“航空”。我如何修复这个风格,使我的按钮都看起来空气动力学,但也有这些小的变化?我宁愿不必设置 Style

    更新

    我应该在第一时间提到这一点,但如果我尝试使用 BasedOn ,如下所示,我得到一个 StackOverflowException

    <Style BasedOn="{StaticResource {x:Type Button}}" TargetType="{x:Type Button}">
        <Setter Property="Padding" Value="3" />
        <Setter Property="FontWeight" Value="Bold" />
    </Style>
    

    这通常是可行的,但在合并了Aero字典的情况下就不行了。如果我把那些字典注释掉,例外就会消失。

    更新2

    如果我加上 x:Key 属性并手动设置样式,它可以正常工作(带有填充和粗体的Aero样式),但是正如我所说的,我更希望样式自动全局应用于所有按钮。

    更新3

    3 回复  |  直到 15 年前
        1
  •  8
  •   Dan    8 年前

    我希望你同时找到了解决办法。对其他人来说, here is one 解决方法,以及 here is another

    更新

    我想出了一个解决办法:

    <Style TargetType="TextBox" BasedOn="{Common:StaticApplicationResource {x:Type TextBox}}">
        <Setter Property="Height" Value="21"/>
    </Style>
    

    在哪里? StaticApplicationResource 是我编写的一个自定义MarkupExtension,它只调用 TryFindResource :

    [MarkupExtensionReturnType(typeof(object))]
    public class StaticApplicationResource : MarkupExtension
    {
        public StaticApplicationResource(object pResourceKey)
        {
            mResourceKey = pResourceKey;
        }
    
        private object _ResourceKey;
    
        [ConstructorArgument("pResourceKey")]
        public object mResourceKey
        {
            get { return _ResourceKey; }
            set { _ResourceKey = value; }
        }
    
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            if (mResourceKey == null)
                return null;
    
            object o = Application.Current.TryFindResource(mResourceKey);
    
            return o;
        }
    }
    

    Here is an excellent article 向您展示如何从代码加载资源字典(以及框架自动解析的资源字典)。

        2
  •  0
  •   Bolu    9 年前

    <Style x:Key="_buttonStyleBase"
           BasedOn="{StaticResource {x:Type Button}}"
           TargetType="{x:Type Button}"> 
        <Setter Property="Padding" Value="3" /> 
        <Setter Property="FontWeight" Value="Bold" /> 
    </Style>
    
    <Style TargetType="{x:Type Button}"
           BasedOn="{StaticResource _buttonStyleBase}" />
    
        3
  •  0
  •   Bolu    9 年前

    使用BasedOn属性从Aero样式继承特性。这应该能解决你的问题。

    <Style
      BasedOn="{StaticResource {x:Type Button}}"
      TargetType="{x:Type Button}"> 
        <Setter Property="Padding" Value="3" /> 
        <Setter Property="FontWeight" Value="Bold" /> 
    </Style>