C# 更改已禁用listView的背景

C# 更改已禁用listView的背景,c#,wpf,listview,C#,Wpf,Listview,我想设置禁用列表视图的样式。但无论我做什么,禁用时listView都保持默认样式 我已经尝试过的: 根据此线程将系统颜色设置为透明: 我还在上查看了listView的样式,并重新定义了DisabledBorderLightColor 当然,我创造了一个触发器 所有这些尝试都可以在窗口的资源部分看到: <Window.Resources> <Style TargetType="{x:Type ListView}"> <Style.Resourc

我想设置禁用列表视图的样式。但无论我做什么,禁用时listView都保持默认样式

我已经尝试过的

  • 根据此线程将系统颜色设置为透明:
  • 我还在上查看了listView的样式,并重新定义了
    DisabledBorderLightColor
  • 当然,我创造了一个触发器
  • 所有这些尝试都可以在窗口的资源部分看到:

    <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>
    
    
    #FF00FF00
    
    这是相应的c#:

    公共部分类主窗口:窗口
    {
    公共主窗口()
    {
    初始化组件();
    var objectsToShow=新列表
    {
    新对象显示
    {
    Property1=“Content1Property1”,
    Property2=“Content1Property2”
    },
    新对象显示
    {
    Property1=“Content2Property1”,
    Property2=“Content2Property2”
    },
    };
    _listView.ItemsSource=objectsToShow;
    }
    私有void SelectItem(对象发送者,SelectionChangedEventArgs e)
    {
    _listView.IsEnabled=false;
    }
    private void UnselectItem(对象发送方,RoutedEventArgs e)
    {
    _listView.SelectedItem=null;
    _listView.IsEnabled=true;
    }
    }
    公共类ObjectToShow
    {
    公共字符串属性1{get;set;}
    公共字符串属性2{get;set;}
    }
    

    我只希望listView在禁用时具有红色背景。如何实现这一点?

    我认为您必须定义自己的控件模板,以覆盖此控件的默认ListView模板

    或者,如果内置控件笔刷满足您的需要,您也可以尝试:

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

    将您的
    列表视图
    样式更改为以下样式:

        <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>
    
    
    
    当你不得不覆盖一个控件的整个模板,只是为了改变它的一小部分,这是非常烦人的。。。但有时你别无选择


    在这种情况下,由于更改背景颜色的触发器位于ControlTemplate中,因此无法使用样式触发器覆盖它。

    我已经尝试过(我所做的事情列表中的第一点)但没有效果。请您提供
    xmlns=
    -针对
    Microsoft\u Windows\u主题的声明。我真的需要添加对PresentationFramework.Aero.dll的引用吗?或者你可以用一个常规边框替换chrome,并为鼠标移动行为添加触发器等等。请记住,WPF控件是无外观的,并且ListView不明确支持自定义禁用的背景。这只是一个控件如何由默认主题模板化的问题,您希望覆盖它。。。所以你必须,无论如何,创建一个新的模板。我刚刚使用了默认的W7 Aero模板作为基础,但这完全取决于您。或者。。。您可以尝试覆盖它使用的系统笔刷。。。但是,控件使用的笔刷完全取决于主题。毕竟,覆盖笔刷是一种技巧。这就是为什么我建议这样做,因为这是在WPF中唯一“正确”的方法。你可能是对的。但是我仍然在努力解决这样一个事实:
    ListView
    应该是无外观的。我的意思是,它有像
    Background
    这样的属性。这些是为了好看!我可以联系起来,相信我:P让我们只说控件不定义它们自己的外观,但它们建议它们应该如何使用一些属性。。。然后是“圣堂武士”是否使用它们,以及如何使用它们
        <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>