简体   繁体   中英

Change background of disabled listView

I want to set the style of a disabled ListView. But no matter what I do, the listView stays at the default style when disabled.

What I've already tried :

  1. Setting the System Color SystemColors.ControlBrushKey to Transparent according to this thread: Change disabled listbox background to gray
  2. I also looked at the style of the listView at MSDN and redefined the DisabledBorderLightColor
  3. And of course I created a Trigger

All these tries can be seen in the resource-section of the window:

<Window.Resources>
    <Style TargetType="{x:Type ListView}">
        <Style.Resources>
            <Color x:Key="DisabledBorderLightColor">#FF00FF00</Color>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Transparent"/>
        </Style.Resources>
        <Setter Property="Background" Value="Green"/>
        <Style.Triggers>
            <Trigger Property="IsEnabled" Value="False">
                <Setter Property="Background" Value="Red"/>
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="25"/>
        <RowDefinition/>
    </Grid.RowDefinitions>
    <Button Content="Unselect" Click="UnselectItem"></Button>
    <ListView x:Name="_listView" 
              Grid.Row="1"
              d:DataContext="{d:DesignData ObjectToShow}" 
              IsEnabled="True"
              SelectionChanged="SelectItem">
        <ListView.View>
            <GridView>
                <GridView.Columns>
                    <GridViewColumn Header="Header1" DisplayMemberBinding="{Binding Property1}"/>
                    <GridViewColumn Header="Header2" DisplayMemberBinding="{Binding Property2}"/>
                </GridView.Columns>
            </GridView>
        </ListView.View>
    </ListView>
</Grid>

This is the according c#:

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();

        var objectsToShow = new List<ObjectToShow>
        {
            new ObjectToShow
            {
                Property1 = "Content1Property1",
                Property2 = "Content1Property2"
            },
            new ObjectToShow
            {
                Property1 = "Content2Property1",
                Property2 = "Content2Property2"
            },
        };

        _listView.ItemsSource = objectsToShow;
    }

    private void SelectItem(object sender, SelectionChangedEventArgs e)
    {
        _listView.IsEnabled = false;
    }

    private void UnselectItem(object sender, RoutedEventArgs e)
    {
        _listView.SelectedItem = null;
        _listView.IsEnabled = true;
    }
}

public class ObjectToShow
{
    public string Property1 { get; set; }

    public string Property2 { get; set; }
}

I just want the listView to have the Red background when it is disabled. How can I achieve that?

Change your ListView Style to something like this:

    <Style TargetType="{x:Type ListView}">
        <Setter Property="Background" Value="Green"/>
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListView}">
                    <Microsoft_Windows_Themes:ListBoxChrome x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" RenderMouseOver="{TemplateBinding IsMouseOver}" RenderFocused="{TemplateBinding IsKeyboardFocusWithin}" SnapsToDevicePixels="true">
                        <ScrollViewer Padding="{TemplateBinding Padding}" Style="{DynamicResource {x:Static GridView.GridViewScrollViewerStyleKey}}">
                            <ItemsPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                        </ScrollViewer>
                    </Microsoft_Windows_Themes:ListBoxChrome>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsGrouping" Value="true">
                            <Setter Property="ScrollViewer.CanContentScroll" Value="false"/>
                        </Trigger>
                        <Trigger Property="IsEnabled" Value="false">
                            <!-- Here comes your custom disabled background color -->
                            <Setter Property="Background" TargetName="Bd"
                                    Value="Red"/>
                            <!-- Here comes your custom disabled background color -->
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    </Style>

It's pretty annoying when you have to overwrite the whole Template of a Control, just to change a tiny bit of it... But sometimes you're left little choice.

In this case, since the Trigger that changes the Background color is in the ControlTemplate, there's no way to override it with a Style Trigger.

I think you have to define your own control template overriding the default ListView template for this one.

Or you can try if the built-in control brush does what you need:

<ListView.Style>
    <Style TargetType="{x:Type ListView}">
        <Style.Resources>
            <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="Crimson"/>
        </Style.Resources>
    </Style>
</ListView.Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM