基于PresentationFramework.Aero 覆盖WPF TextBox 中的默认样式

发布于 2024-07-15 05:04:39 字数 1047 浏览 1 评论 0原文

我想使用 Aero 文本框样式,但仍然覆盖一些属性。 我尝试通过以下方式实现此目的:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Margin" Value="2" />
        <Setter Property="Padding" Value="2" />
    </Style>
</ResourceDictionary>

但是,这会在启动我的应用程序时导致 StackOverflowException 。 当我删除对PresentationFramework.Aero的引用时,这可以工作,但我得到了默认的操作系统样式,这使得应用程序变得丑陋。 ;)

所以,实际上:如果我想覆盖所有文本框上的某些样式,我无法获得 Aero 外观。 如果我想要 Aero 外观,我无法覆盖任何样式。 僵局。

有办法解决这个问题吗?

I want to use the Aero textbox styling, but still override some properties. I try to accomplish this by:

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

    <ResourceDictionary.MergedDictionaries>
        <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml" />
    </ResourceDictionary.MergedDictionaries>

    <Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
        <Setter Property="Margin" Value="2" />
        <Setter Property="Padding" Value="2" />
    </Style>
</ResourceDictionary>

However, this results in a StackOverflowException when starting my app.
When I remove the reference to PresentationFramework.Aero, this works but I get the default OS styling, which makes the app ugly. ;)

So, in effect: if I want to override some style on all my textboxes I cannot get the Aero look. If I want the Aero look, I cannot override any styling. Deadlock.

Any way to solve this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

抱着落日 2024-07-22 05:04:39

如果将 Style 作为较低级别的资源,而不是放在同一个 ResourceDictionary 中,它似乎可以工作:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    <Border BorderBrush="Blue" BorderThickness="3">
        <Border.Resources>
            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                <Setter Property="Margin" Value="2" />
                <Setter Property="Padding" Value="2" />
            </Style>
        </Border.Resources>
        <TextBox />
    </Border>
</Grid>

It seems to work if you put the Style as a lower-level resource, instead of in the same ResourceDictionary:

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/PresentationFramework.Aero, Version=3.0.0.0, Culture=Neutral, PublicKeyToken=31bf3856ad364e35, ProcessorArchitecture=MSIL;component/themes/aero.normalcolor.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Grid.Resources>
    <Border BorderBrush="Blue" BorderThickness="3">
        <Border.Resources>
            <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
                <Setter Property="Margin" Value="2" />
                <Setter Property="Padding" Value="2" />
            </Style>
        </Border.Resources>
        <TextBox />
    </Border>
</Grid>
生死何惧 2024-07-22 05:04:39

与接受的答案中的代码不同,此代码允许使用资源字典作为样式。 无耻地从 http://social.msdn 窃取。 microsoft.com/forums/en-US/wpf/thread/3c66adb7-fd26-40c7-8404-85f6fefbd392/Vivien Ruitz回答

<!--App.xaml-->
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/MyAppli;component/Resources/Themes/StyleDictionary.xaml"/>  
            <ResourceDictionary Source="/MyAppli;component/Resources/Themes/ApplyStyleDictionary.xaml"/>  
            ...  
        </ResourceDictionary.MergedDictionaries> 

<!--StyleDictionary.xaml-->
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" /> 
        </ResourceDictionary.MergedDictionaries> 
        <Style x:Key="ButtonStyleToApply" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" > 
            ...  <!--Extend the aero style here-->
        </Style> 

<!--ApplyStyleDictionary.xaml-->
        <Style TargetType="Button" BasedOn="{StaticResource ButtonStyleToApply}"/>

Unlike the code in accepted answer this one allows using resource dictionary for styles. Shamelessly stolen from http://social.msdn.microsoft.com/forums/en-US/wpf/thread/3c66adb7-fd26-40c7-8404-85f6fefbd392/ answered by Vivien Ruitz

<!--App.xaml-->
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/MyAppli;component/Resources/Themes/StyleDictionary.xaml"/>  
            <ResourceDictionary Source="/MyAppli;component/Resources/Themes/ApplyStyleDictionary.xaml"/>  
            ...  
        </ResourceDictionary.MergedDictionaries> 

<!--StyleDictionary.xaml-->
        <ResourceDictionary.MergedDictionaries> 
            <ResourceDictionary Source="/PresentationFramework.Aero;V3.0.0.0;31bf3856ad364e35;component/themes/aero.normalcolor.xaml" /> 
        </ResourceDictionary.MergedDictionaries> 
        <Style x:Key="ButtonStyleToApply" TargetType="Button" BasedOn="{StaticResource {x:Type Button}}" > 
            ...  <!--Extend the aero style here-->
        </Style> 

<!--ApplyStyleDictionary.xaml-->
        <Style TargetType="Button" BasedOn="{StaticResource ButtonStyleToApply}"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文