gpt4 book ai didi

WPF 默认主题和自定义样式不能一起工作

转载 作者:行者123 更新时间:2023-12-04 14:42:46 25 4
gpt4 key购买 nike

嘿,我有一个针对 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">

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

<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>

最佳答案

试试这个:

<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>

关于WPF 默认主题和自定义样式不能一起工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5062468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com