禁用/禁用ListBox时覆盖背景颜色

时间:2014-05-15 23:43:34

标签: c# wpf listbox styles itemcontainerstyle

我想拥有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设置为null(请参阅此answer);将一些“非活动”颜色刷System.Colors设置为透明,并将ScrollBar颜色设置为透明(请参阅此link),但没有任何效果......

  • 我错过了什么?
  • 您知道哪个颜色可能是问题(请参阅此post)?
  • 状态是什么?禁用?不活跃?

1 个答案:

答案 0 :(得分:2)

通过全局更改颜色很难做到,因为你会从其他控件中获得混淆效果。

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

 <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>

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

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

发布后,这一切都在4.5 ...

下编译并运行

有趣的链接是......

ListBox Styles and Templates

Color values

用于检查WPF dll中模板的非常有用的实用程序:http://www.sellsbrothers.com/tools/ShowMeTheTemplate.zip