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

标签 wpf xaml

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

相关文章:

c# - 是否可以在 wpf 中使用 silverlight 引用?

c# - 单击DataGridHyperlinkColumn后,WPF MVVM获取行对象

c# - 在两个 DataGrid 之间共享 1 个滚动条

c# - 防止触发 CheckBox Checked 事件

c# - 从 DataGrid 中删除空格

c# - 通过按钮在 ListView 中进行单选

c# - UWP 应用程序中的自引用泛型类型约束和 XAML

c# - 将 MahApps.Metro 主题和重音应用于其他控件或矩形

.net - 错误 "Tag does not exist in XML namespace"

wpf - 轨道动画