WPF 默认主题和自定义样式不能一起使用

发布于 2024-10-18 11:35:46 字数 1616 浏览 0 评论 0原文

嘿,我有一个针对 XP 计算机的 WPF 应用程序。问题是我们希望使用 WPF XP luna 主题而不是经典主题运行,并且我们的大多数客户端都在经典模式下运行。我们的客户都是内部的,只是他们的机器配置的是XP classic。

理论上,这就像将其添加到应用程序一样简单:

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

实际上,只要触摸任何样式(例如向 TextBox 添加边距),它们的样式似乎就会恢复到经典主题。

这显示正确(Style Luna):

<TextBox  Width="80" Height="20" />

这显示正确(Style Luna):

<TextBox Width="80" Height="20" Background="Brown">

这显示不正确(Style Classic),请注意,现在样式块中有很多节点并不重要 - 零足以混淆事物:

<TextBox.Style><Style></Style></TextBox.Style></TextBox>

长和短,覆盖默认的操作系统主题似乎阻止了样式的进一步使用。我在这里缺少什么?

查看故事 80% 的精选答案。完整的故事是这样的:我还必须提供“BasedOn”设置。不幸的是,这意味着我们无法在不引起循环的情况下覆盖文本框。定义:

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

将导致错误:“在属性表达式中检测到循环”。 我选择解决这个问题的方法是在各处强制命名样式。例如:

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

<Style x:Key="Text25Chars" TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBase}">
    <Setter Property="Margin" Value="0,2,0,2" />
    :
</Style>

Hey, I have a WPF application targeted at XP machines. The problem is that we wish to run with a WPF XP luna theme rather than classic and most of our clients run in classic mode. Our clients are all internal, it's just that their machines were configured with XP classic.

In theory, this is as simple as adding this to the application:

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

In practice, as soon as a touch any of the styles (say add a margin to TextBox), they styles seem to revert back to classic theme.

This displays correctly (Style Luna):

<TextBox  Width="80" Height="20" />

This displays correctly (Style Luna):

<TextBox Width="80" Height="20" Background="Brown">

This displays incorrectly (Style Classic), note it does not matter now many nodes there are in the style block - zero is enough to confuse things:

<TextBox.Style><Style></Style></TextBox.Style></TextBox>

Long and short, overriding the default OS theme seems to preclude further use of styles. What am I missing here?

See the select answer for 80% of the story. The full story is this: I must supply the ‘BasedOn’ setting as well. Unfortunately, this means we can not override, say textbox, without causing a loop. Defining:

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

would result in the error: "a loop was detected in the property expression".
The way I chose to get around this was to force named styles everywhere. For example:

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

<Style x:Key="Text25Chars" TargetType="{x:Type TextBox}" BasedOn="{StaticResource TextBase}">
    <Setter Property="Margin" Value="0,2,0,2" />
    :
</Style>

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

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

发布评论

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

评论(1

怪我闹别瞎闹 2024-10-25 11:35:46

试试这个:

<TextBox>
    <TextBox.Style>
        <Style BasedOn="{StaticResource {x:Type TextBox}}">
        </Style>
    </TextBox.Style>
</TextBox>

编辑:
忘记了 TargetType,这对我有用:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral,
                            PublicKeyToken=31bf3856ad364e35,
                            ProcessorArchitecture=MSIL;component/themes/luna.normalcolor.xaml" />
    </Window.Resources>
        <TextBox>
            <TextBox.Style>
                <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
                    <Setter Property="Foreground" Value="Blue" />
                </Style>
            </TextBox.Style>
            tototototottototo
        </TextBox>
</Window>

try this:

<TextBox>
    <TextBox.Style>
        <Style BasedOn="{StaticResource {x:Type TextBox}}">
        </Style>
    </TextBox.Style>
</TextBox>

edit:
forgot the TargetType, this works for me:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">

    <Window.Resources>
        <ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral,
                            PublicKeyToken=31bf3856ad364e35,
                            ProcessorArchitecture=MSIL;component/themes/luna.normalcolor.xaml" />
    </Window.Resources>
        <TextBox>
            <TextBox.Style>
                <Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
                    <Setter Property="Foreground" Value="Blue" />
                </Style>
            </TextBox.Style>
            tototototottototo
        </TextBox>
</Window>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文