【问题标题】:Change background of disabled listView更改禁用列表视图的背景
【发布时间】:2015-09-28 22:09:06
【问题描述】:

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

我已经尝试过的

  1. 根据此线程将系统颜色SystemColors.ControlBrushKey 设置为TransparentChange disabled listbox background to gray
  2. 我也在MSDN查看了listView的样式,重新定义了DisabledBorderLightColor
  3. 当然我还创建了一个触发器

所有这些尝试都可以在窗口的资源部分中看到:

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

这是相应的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; }
}

我只希望 listView 在禁用时具有红色背景。我怎样才能做到这一点?

【问题讨论】:

    标签: c# wpf listview


    【解决方案1】:

    将您的 ListView 样式更改为以下内容:

        <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 中,因此无法使用样式触发器覆盖它。

    【讨论】:

    • 您能否为Microsoft_Windows_Themes 提供xmlns= 声明。我真的必须添加对PresentationFramework.Aero.dll 的引用吗?
    • 那个,或者您可以用常规边框替换镶边,并为鼠标悬停行为添加触发器以及您自己的所有操作。请记住,WPF 控件是无外观的,并且 ListView 不明确支持自定义禁用背景。这只是您的默认主题如何模板化控件的问题,并且您想要覆盖它......所以您必须创建一个新模板。我刚刚使用了默认的 W7 Aero 模板作为基础,但这完全取决于您。
    • 或者...您可以尝试覆盖它使用的系统 Brush...但是再一次,控件使用哪个 Brush 完全取决于主题。毕竟,覆盖画笔是一种黑客行为。这就是我建议这样做的原因,因为它是 WPF 中唯一“正确”的方法。
    • 你可能是对的。但我仍然在为ListView 应该是无表情的事实而苦恼。我的意思是,它有像Background 这样的属性。这些是为了好看!
    • 我可以理解,相信我 :P 我们只是说控件没有定义自己的外观,但他们建议他们应该如何看待一些属性.. .然后由“模板”来使用它们,以及如何使用它们
    【解决方案2】:

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

    或者你可以试试内置控制画笔是否满足你的需要:

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

    【讨论】:

    • 我已经尝试过了(我所做的事情列表中的第一点),但无济于事。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-10
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多