【问题标题】:Wrong colors in a WPF application in Windows Server 2012Windows Server 2012 中的 WPF 应用程序中的颜色错误
【发布时间】:2016-03-04 13:23:49
【问题描述】:

以下代码创建一个带有列表框项的窗口,当您将鼠标放在项目上时,列表框项会变为橙色:

<Window x:Class="Orange.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >

    <Window.Resources>
        <Style TargetType="ListBoxItem">
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="Orange" />
                </Trigger>
                </Style.Triggers>
            </Style>
    </Window.Resources>

    <ListBox>
        <ListBoxItem> aaaa </ListBoxItem>
        <ListBoxItem> aaaa </ListBoxItem>
        <ListBoxItem> aaaa </ListBoxItem>
    </ListBox>
</Window>

如果您在除 Windows 2012 Server 之外的任何操作系统上执行它,它都能正常工作。在 Windows 2012 Server 上,背景显示为白色。

有什么想法吗?

【问题讨论】:

  • 这很奇怪。如果您尝试使用#F90 等颜色的直接十六进制值会怎样?

标签: .net wpf xaml colors windows-server-2012


【解决方案1】:

我使用的是 Windows 10(版本 1511),但它也无法在我的系统上运行。

我使用 VS 2015 的 Document Outline 为 ListBoxItem 创建了一个模板。我将它修改为橙色背景,它适用于 Win10(并且,我假设是 Server 2012)。

<Window x:Class="WpfApplication20.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" >
    <Window.Resources>
        <Style TargetType="ListBoxItem">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListBoxItem}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                            <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsMouseOver" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="Orange"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="#A826A0DA"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelectionActive" Value="False"/>
                                    <Condition Property="IsSelected" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="#3DDADADA"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="#FFDADADA"/>
                            </MultiTrigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Selector.IsSelectionActive" Value="True"/>
                                    <Condition Property="IsSelected" Value="True"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="#3D26A0DA"/>
                                <Setter Property="BorderBrush" TargetName="Bd" Value="#FF26A0DA"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="TextElement.Foreground" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
    </Window.Resources>
    <ListBox>
        <ListBoxItem>aaaa</ListBoxItem>
        <ListBoxItem>aaaa</ListBoxItem>
        <ListBoxItem>aaaa</ListBoxItem>
    </ListBox>
</Window>

看看 IsMouseOver 是如何触发的,并且它将边框的背景设置为硬编码值?这就是为什么你的风格不起作用......背景可能正在改变,但边框没有使用它!使用 Snoop,我看到 ListBoxItem 的背景确实变为橙色,但正如我所说,背景使用模板触发器中的颜色。

您可能需要做的是创建一种样式,然后以此为基础创建其他样式。我在我的 Win7 VM 上使用 VS 2013 创建了一个基本模板,该模板应该接近您现有样式的预期。把它放在你的 app.xaml 中:

<Style TargetType="ListBoxItem">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="True">
                    <ContentPresenter ContentTemplate="{TemplateBinding ContentTemplate}" Content="{TemplateBinding Content}" ContentStringFormat="{TemplateBinding ContentStringFormat}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                </Border>
                <ControlTemplate.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="True"/>
                            <Condition Property="Selector.IsSelectionActive" Value="False"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="False">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

然后根据它更改你的样式,如下所示:

<Style TargetType="ListBoxItem" BasedOn="{StaticResource {x:Type ListBoxItem}}">
    <Style.Triggers>
        <Trigger Property="IsMouseOver" Value="True">
            <Setter Property="Background" Value="Orange" />
        </Trigger>
    </Style.Triggers>
</Style>

希望它有所帮助......可能并不理想,但它应该可以工作。

【讨论】:

  • 很高兴知道。我没有尝试过 Windows 10。无论如何我希望我能找到一个不涉及更改代码的解决方案。我们有大量的自定义样式,因为该应用程序旨在提供丰富的用户体验。
  • 嗯,对我来说听起来像是一个错误。你认为我应该以 M$ 填写一份吗?
  • 你可以,但我认为这个问题始于 Windows 8,所以我很确定他们知道。您在 Windows 8 上尝试过吗? Server 2012 基于 Win 8,所以我认为它不会工作。
  • 这就是我的想法,但在 Windows 8 和 8.1 上运行良好。这让我很困惑。
  • 不确定 Win 8/8.1 为何有效。我没有可用的 ATM 之一来查看 ListBoxItem 模板。我用可能有帮助的变通方法更新了我的答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-22
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
相关资源
最近更新 更多