WPF默认主题和自定义样式不能一起工作
嘿,我有一个针对XP机器的WPF应用程序。问题是,我们希望使用WPF XP luna主题而不是classic运行,我们的大多数客户机都在classic模式下运行。我们的客户都是内部的,只是他们的机器配置了XP classic 理论上,这与将其添加到应用程序中一样简单:WPF默认主题和自定义样式不能一起工作,wpf,xaml,Wpf,Xaml,嘿,我有一个针对XP机器的WPF应用程序。问题是,我们希望使用WPF XP luna主题而不是classic运行,我们的大多数客户机都在classic模式下运行。我们的客户都是内部的,只是他们的机器配置了XP classic 理论上,这与将其添加到应用程序中一样简单: <ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf
<ResourceDictionary Source="/PresentationFramework.Luna, Version=3.0.0.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35,
ProcessorArchitecture=MSIL;component/themes/luna.normalcolor.xaml" />
实际上,只要触摸任何一种样式(比如在文本框中添加一个边距),它们的样式就好像回到了经典主题
这将正确显示(样式Luna):
这将正确显示(样式Luna):
这显示不正确(Style Classic),请注意,现在样式块中有许多节点已无关紧要-零足以混淆:
<TextBox.Style><Style></Style></TextBox.Style></TextBox>
不管是长是短,覆盖默认操作系统主题似乎排除了样式的进一步使用。我错过了什么
请参阅故事80%的选择答案。整个故事是这样的:我还必须提供“BasedOn”设置。不幸的是,这意味着我们不能覆盖,比如说文本框,而不引起循环。定义:
:
将导致错误:“在属性表达式中检测到循环”。
我选择的解决方法是在任何地方强制使用命名样式。例如:
:
:
试试这个:
<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>
托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托托
你是个英雄。添加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>
<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>