WPF ListView 关闭选择 [英] WPF ListView turn off selection

查看:33
本文介绍了WPF ListView 关闭选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以关闭WPFListView的选择,这样当用户点击行时,该行不突出显示?

Is it possible to turn off the selection of a WPF ListView, so when user clicks row, the row is not highlighted?

我希望第 1 行在点击时看起来像第 0 行.

I would like the row 1 to look just like row 0 when clicked.

可能相关:我可以设计悬停/选择的外观吗?例如.用自定义纯色替换蓝色渐变悬停外观(第 3 行).我找到了 this这个,不幸的是没有帮助.

Possibly related: can I style the look of the hover / selection? Eg. to replace the blue gradient hover look (line 3) with a custom solid color. I have found this and this, unfortunately not helping.

(在不使用 ListView 的情况下实现相同的效果也是可以接受的.我只是希望能够像 ListView 一样使用逻辑滚动和 UI 虚拟化)

(Achieving the same without using ListView is acceptable too. I'd just like to be able to use logical scrolling and UI virtualization as ListView does)

ListView 的 XAML 是:

The XAML for ListView is:

<ListView Height="280" Name="listView">
    <ListView.Resources>
        <!-- attempt to override selection color -->
        <SolidColorBrush x:Key="{x:Static SystemColors.HighlightColorKey}"
                         Color="Green" />
    </ListView.Resources>
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                <!-- more columns -->
            </GridView.Columns>
        </GridView>
     </ListView.View>
</ListView>

推荐答案

Per Martin Konicek 的评论,以最简单的方式完全禁用项目的选择:

Per Martin Konicek's comment, to fully disable the selection of the items in the simplest manner:

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="ListViewItem">
            <Setter Property="Focusable" Value="false"/>
        </Style>
    </ListView.ItemContainerStyle>
    ...
</ListView>

但是,如果您仍然需要 ListView 的功能,例如能够选择一个项目,那么您可以像这样在视觉上禁用所选项目的样式:

However if you still require the functionality of the ListView, like being able to select an item, then you can visually disable the styling of the selected item like so:

您可以通过多种方式执行此操作,从更改 ListViewItem 的 ControlTemplate 仅设置样式(更容易).您可以使用 ItemContainerStyle 为 ListViewItems 创建样式 并在选择时关闭"背景和边框画笔.

You can do this a number of ways, from changing the ListViewItem's ControlTemplate to just setting a style (much easier). You can create a style for the ListViewItems using the ItemContainerStyle and 'turn off' the background and border brush when it is selected.

<ListView>
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <Style.Triggers>
                <Trigger Property="IsSelected"
                         Value="True">
                    <Setter Property="Background"
                            Value="{x:Null}" />
                    <Setter Property="BorderBrush"
                            Value="{x:Null}" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
    ...
</ListView>

此外,除非您有其他方式在项目被选中(或仅用于测试)时通知用户,否则您可以添加一列来表示值:

Also, unless you have some other way of notifying the user when the item is selected (or just for testing) you can add a column to represent the value:

<GridViewColumn Header="IsSelected"
                DisplayMemberBinding="{Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type ListViewItem}}, Path=IsSelected}" />

这篇关于WPF ListView 关闭选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆