【问题标题】:Override the background color when ListBox is disabled/inactived当 ListBox 被禁用/不活动时覆盖背景颜色
【发布时间】:2014-05-15 23:43:33
【问题描述】:

我希望拥有ListBox 背景的所有内容透明。不专注/专注/随便……

我覆盖了ListBox 及其ItemContainerStyle 的样式,但当ListBox 被另一个元素禁用时,我仍然有背景颜色...

<Style TargetType="{x:Type ListBox}" x:Key="MyListBoxStyle">
    <Setter Property="Background" Value="{DynamicResource TransparentBackgroundBrush}"/>
    <Setter Property="BorderBrush" Value="{DynamicResource TransparentBackgroundBrush}"/>
    <Setter Property="SelectionMode" Value="Single"/>
    <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Disabled"/>
    <Setter Property="ItemContainerStyle" Value="{DynamicResource MyListBoxItemStyle}"/>
</Style>



<Style TargetType="{x:Type ListBoxItem}" x:Key="MyListBoxItemStyle">
    <Style.Resources>
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="{DynamicResource TransparentColor}" />
        <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="{DynamicResource TransparentColor}"/>
    </Style.Resources>
</Style>

我还尝试将FocusVisualStyle 设置为空(请参阅此answer);将一些“非活动”颜色 BrushSystem.Colors 设置为透明并将 ScrollBar 颜色设置为透明(请参阅此link),但没有任何效果...

  • 我错过了什么?
  • 您知道哪种颜色可能是问题所在吗(请参阅post)?
  • 什么是状态?禁用?无效?

【问题讨论】:

  • 您还需要将 ListBox 的 SystemColors.ControlBrushKey 设置为透明,而不仅仅是 ListBoxItem
  • ListBox 控件模板告诉我们它是 DisabledControlLightColor,它又被定义为#FFE8EDF9。您需要覆盖这些
  • @GayotFow,你从哪里得到的?官方标准 WPF 主题/样式没有/使用“DisabledControlLightColor”...
  • @elgonzo,它在控制模板中。你需要覆盖它。我只是将其批发导入并通过反复试验进行更改,直到您得到您想要的为止。
  • @GayotFow,不,我问你从哪里得到的。我知道的唯一地方是 MSDN 文档中的示例,但这些示例是关于使用自定义资源定义您自己的自定义模板,而不是关于覆盖标准主题/样式的资源(包括标准控制模板)。

标签: c# wpf listbox styles itemcontainerstyle


【解决方案1】:

很难通过全局更改颜色来做到这一点,因为您会从其他控件中获得令人困惑的效果。

更改列表框的背景颜色以匹配给定状态,例如非活动或禁用,可以通过获取像这样的普通列表框模板来实现...

 <Style x:Key="{x:Type ListBox}" TargetType="ListBox">
        <Setter Property="SnapsToDevicePixels"  Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="ScrollViewer.HorizontalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.VerticalScrollBarVisibility" Value="Auto" />
        <Setter Property="ScrollViewer.CanContentScroll" Value="true" />
        <Setter Property="MinWidth" Value="120" />
        <Setter Property="MinHeight" Value="95" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBox">
                    <Border Name="Border" BorderThickness="1" CornerRadius="2">
                        <Border.Background>
                            <SolidColorBrush Color="{StaticResource ControlLightColor}" />
                        </Border.Background>
                        <Border.BorderBrush>
                            <SolidColorBrush Color="{StaticResource BorderMediumColor}" />
                        </Border.BorderBrush>
                        <ScrollViewer Margin="0" Focusable="false">
                            <StackPanel Margin="2" IsItemsHost="True" />
                        </ScrollViewer>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter TargetName="Border" Property="Background">
                                <Setter.Value>
                                    <SolidColorBrush Color="{StaticResource DisabledControlLightColor}" />
                                </Setter.Value>
                            </Setter>
                            <Setter TargetName="Border" Property="BorderBrush">
                                <Setter.Value>
                                    <SolidColorBrush Color="{DynamicResource DisabledBorderLightColor}" />
                                </Setter.Value>
                            </Setter>
                        </Trigger>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false" />
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>
    <Style x:Key="{x:Type ListBoxItem}" TargetType="ListBoxItem">
        <Setter Property="SnapsToDevicePixels" Value="true" />
        <Setter Property="OverridesDefaultStyle" Value="true" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="ListBoxItem">
                    <Border x:Name="Border" Padding="2" SnapsToDevicePixels="true">
                        <Border.Background>
                            <SolidColorBrush Color="Transparent" />
                        </Border.Background>
                        <VisualStateManager.VisualStateGroups>
                            <VisualStateGroup x:Name="SelectionStates">
                                <VisualState x:Name="Unselected" />
                                <VisualState x:Name="Selected">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                            Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource SelectedBackgroundColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="SelectedUnfocused">
                                    <Storyboard>
                                        <ColorAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                            Storyboard.TargetProperty="(Panel.Background).(SolidColorBrush.Color)">
                                            <EasingColorKeyFrame KeyTime="0"
                                     Value="{StaticResource SelectedUnfocusedColor}" />
                                        </ColorAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                            </VisualStateGroup>
                        </VisualStateManager.VisualStateGroups>
                        <ContentPresenter />
                    </Border>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

...并将其添加到您的窗口资源中。请注意,模板引用了一组颜色,这些颜色在对象图中被进一步定义为...

    <Color x:Key="DisabledControlLightColor">#FFE8EDF9</Color>
    <Color x:Key="DisabledControlDarkColor">#FFC5CBF9</Color>
    <Color x:Key="DisabledForegroundColor">#FF888888</Color>
    <Color x:Key="ControlLightColor">White</Color>
    <Color x:Key="ControlMediumColor">#FF7381F9</Color>
    <Color x:Key="ControlDarkColor">#FF211AA9</Color>
    <Color x:Key="BorderLightColor">#FFCCCCCC</Color>
    <Color x:Key="BorderMediumColor">#FF888888</Color>
    <Color x:Key="BorderDarkColor">#FF444444</Color>
    <Color x:Key="SelectedBackgroundColor">#FFC5CBF9</Color>
    <Color x:Key="SelectedUnfocusedColor">#FFDDDDDD</Color>

... 这些可以替换为透明的 WPF 值,即 #00FFFFFF。这会将 ALL 颜色更改为透明,只要模板中存在引用。 StoryBoard 颜色是指系统颜色,需要单独更改。或者完全删除故事板。

一旦您获得所需样式的工作模型,您就可以有选择地向资源和样式列表框添加一个键。

正如发布的那样,这一切都在 4.5 下编译并运行干净...

有趣的链接是...

ListBox Styles and Templates

Color values

还有一个非常有用的工具,用于检查 WPF dll 中的模板:http://www.sellsbrothers.com/tools/ShowMeTheTemplate.zip

【讨论】:

  • 您回答了我关心的一个问题,即全局颜色的变化。我不喜欢覆盖全局颜色,所以你的回答避免了我!非常感谢!
  • @MyRestlessDream,你打算用模板策略吗?
  • 是的,我是。它完全符合我的要求,而无需触及 SystemColors,因此非常完美!
猜你喜欢
  • 1970-01-01
  • 2013-02-09
  • 1970-01-01
  • 1970-01-01
  • 2019-10-14
  • 2018-05-26
  • 2018-11-12
  • 1970-01-01
相关资源
最近更新 更多