WPF:Style.Setter 更改 ComboBox 颜色,不会改回来

WPF: Style.Setter Changes ComboBox Colors, Won't Change Back

我有一个 ComboBox,我在可见性 Style.Setter 上隐藏和显示 属性:

<ComboBox ItemsSource="{Binding ElementName=BVTWindow, Path=DataContext.AreaList}" SelectedItem="{Binding Path=Area}">
    <ComboBox.Style>
        <Style TargetType="{x:Type ComboBox}">
            <Style.Setters>
                <Setter Property="Visibility" Value="Collapsed" />
            </Style.Setters>
            <Style.Triggers>
                <DataTrigger Binding="{Binding ElementName=BVTWindow, Path=DataContext.IdentitySelection}" Value="Test Management">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </ComboBox.Style>
</ComboBox>

效果很好。不幸的是,应用 setter 样式(出于某种原因)会从 ComboBox 中删除颜色主题。现在它是一个带有白色文本的浅灰色框,几乎不可读。

我尝试将 Foreground 和 Background 属性添加到 ComboBox 标记,但没有效果。

我还尝试将 Setter 前景和背景属性添加到 DataTrigger,同样没有效果。

我需要阻止主题从框中删除,手动设置文本颜色,或手动设置背景颜色。

我读到一篇文章说 Windows 8(我正在使用的)会干扰 ComboBox 样式,但没有一个易于理解的解决方案。

任何建议都会对我有很大帮助。提前致谢。

这篇文章解释得很好:

http://social.technet.microsoft.com/wiki/contents/articles/24240.changing-the-background-color-of-a-combobox-in-wpf-on-windows-8.aspx

基本上,Windows 8 对 ComboBox 有不同的设置,它破坏了一些样式属性。您仍然可以设置它的样式,但您必须右键单击 ComboBox 并选择 "edit template"。这将在您的项目视图中生成大量 xaml(每种可能的状态组合的配色方案),但您可能不需要所有这些。我玩评论/取消评论部分来弄清楚每个触发器和 Setter 受到的影响,然后设置我的颜色并杀死额外的部分。

我有一个新的解决方案,它比我以前的想法更干净、更快、更简单、更漂亮:

只需将 ComboBox 包裹在 WrapPanel 或 StackPanel 中,然后将样式 Setter 应用于父项。该控件将保留其正确的样式,并且父面板无论如何都应该是透明的,因此样式在那里无关紧要。最坏的情况是,您得到的 WrapPanel 中只有一项,并且 xaml 中多了两行代码。这也适用于其他控件,因为我只需要使用复选框即可。

这是一个例子:

<WrapPanel>
    <WrapPanel.Style>
        <Style TargetType="{x:Type WrapPanel}">
            <Setter Property="Visibility" Value="Collapsed"/>
            <Style.Triggers>
                <DataTrigger Binding="{Binding DataContext.TesterIdentitySelection.CanEdit, ElementName=BVTWindow}" Value="true">
                    <Setter Property="Visibility" Value="Visible"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </WrapPanel.Style>
    <ComboBox ItemsSource="{Binding Path=DataContext.AreaList, ElementName=BVTWindow}" DisplayMemberPath="Name" SelectedItem="{Binding Path=Area, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" ToolTip="Select the testing area."/>
</WrapPanel>

组合框位于环绕面板内,而环绕面板应用了 show/hide 行为。由于包裹面板本身并没有任何样式(只是一个隐形支架),所以一切看起来都很棒。