WPF 默认主题和自定义样式不能一起使用
嘿,我有一个针对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
试试这个:
编辑:
忘记了 TargetType,这对我有用:
try this:
edit:
forgot the TargetType, this works for me: