【问题标题】:WPF ListView turn off selectionWPF ListView 关闭选择
【发布时间】:2010-11-06 06:40:04
【问题描述】:

是否可以关闭 WPF ListView 的选择,因此当用户单击行时,该行不会突出显示?

我希望第 1 行在单击时看起来像第 0 行。

可能相关:我可以设置悬停/选择的外观吗?例如。用自定义纯色替换蓝色渐变悬停外观(第 3 行)。我找到了thisthis,很遗憾没有帮助。

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

ListView 的 XAML 是:

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

【问题讨论】:

  • 我以前从未在 WPF 中使用过 ListView,但我确信有某种 IsEnabled 属性,如果设置为 false,将禁用整个控件,并且可能会实现你的目标'之后,但我不是 100% 确定。
  • 嗨,是的,有一个 IsEnabled 属性,可以禁用整个 ListView。我需要 ListView 正常工作,只是不显示选择。

标签: wpf listview


【解决方案1】:

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

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

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

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

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

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

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

【讨论】:

  • 没有看到相关的部分,但是你可以做同样的事情,在 IsMouseOver 属性上设置你喜欢的背景和边框。
  • 我现在解决得更好 - 完全禁用选择行。
  • 啊,我以为你的意思是在视觉上关闭选择。如果您根本不希望它们被选中,您可能想看看使用 ItemsControl (因为如果它们不可聚焦,您仍然可以通过代码设置选定的项目),尽管让网格看起来你'将不得不做这样的事情:manicprogrammer.com/cs/blogs/... 另外,如果您还没有,您可能需要查看 WPF Unleashed 一书,因为它提供了对 WPF 和 XAML 的精彩介绍。
  • @MartinKonicek 我知道这是一篇旧帖子,但您的高评价应该是一个答案;)
  • @MartinKonicek 我必须添加BasedOn="{StaticResource {x:Type ListViewItem}}",所以它不会覆盖我的其余网格样式,但我也认为您应该将您的评论作为接受的答案
【解决方案2】:

在上述解决方案的基础上...我会使用 MultiTrigger 让 MouseOver 突出显示在选择后继续工作,这样您的 ListViewItem 的样式将是:

        <ListView.ItemContainerStyle>
            <Style TargetType="ListViewItem">
                <Style.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="True" />
                            <Condition Property="IsMouseOver" Value="False" />
                        </MultiTrigger.Conditions>
                        <MultiTrigger.Setters>
                            <Setter Property="Background" Value="{x:Null}" />
                            <Setter Property="BorderBrush" Value="{x:Null}" />
                        </MultiTrigger.Setters>
                    </MultiTrigger>
                </Style.Triggers>
            </Style>
        </ListView.ItemContainerStyle>

【讨论】:

    【解决方案3】:

    摩尔的回答不起作用,这里的页面:

    Specifying the Selection Color, Content Alignment, and Background Color for items in a ListBox

    解释为什么它不能工作。

    如果您的列表视图只包含基本文本,解决问题的最简单方法是使用透明画笔。

    <Window.Resources>
      <Style TargetType="{x:Type ListViewItem}">
        <Style.Resources>
          <SolidColorBrush x:Key="{x:Static SystemColors.HighlightBrushKey}" Color="#00000000"/>
          <SolidColorBrush x:Key="{x:Static SystemColors.ControlBrushKey}" Color="#00000000"/>
        </Style.Resources>
      </Style>
    </Window.Resources>
    

    如果列表视图的单元格包含组合框等控件,这将产生不良结果,因为它也会改变它们的颜色。要解决这个问题,必须重新定义控件的模板。

      <Window.Resources>
        <Style TargetType="{x:Type ListViewItem}">
          <Setter Property="Template">
            <Setter.Value>
              <ControlTemplate TargetType="{x:Type ListViewItem}">
                <Border SnapsToDevicePixels="True" 
                        x:Name="Bd" 
                        Background="{TemplateBinding Background}" 
                        BorderBrush="{TemplateBinding BorderBrush}" 
                        BorderThickness="{TemplateBinding BorderThickness}" 
                        Padding="{TemplateBinding Padding}">
                  <GridViewRowPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" 
                                        VerticalAlignment="{TemplateBinding VerticalContentAlignment}" 
                                        Columns="{TemplateBinding GridView.ColumnCollection}" 
                                        Content="{TemplateBinding Content}"/>
                </Border>
                <ControlTemplate.Triggers>
                  <Trigger Property="IsEnabled" 
                           Value="False">
                    <Setter Property="Foreground" 
                            Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                  </Trigger>
                </ControlTemplate.Triggers>
              </ControlTemplate>
            </Setter.Value>
          </Setter>
        </Style>
      </Window.Resources>
    

    【讨论】:

    • 这会使我在 ListView 中的所有项目都消失
    【解决方案4】:

    这是来自 Blend 的 ListViewItem 的默认模板:

    默认 ListViewItem 模板:

            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type ListViewItem}">
                        <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                            <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                        </Border>
                        <ControlTemplate.Triggers>
                            <Trigger Property="IsSelected" Value="true">
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                            </Trigger>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="IsSelected" Value="true"/>
                                    <Condition Property="Selector.IsSelectionActive" Value="false"/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Background" TargetName="Bd" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="false">
                                <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
    

    只需删除 IsSelected 触发器和 IsSelected/IsSelectionActive MultiTrigger,将以下代码添加到您的 Style 以替换默认模板,选择时不会有视觉变化。

    关闭 IsSelected 属性的视觉变化的解决方案:

        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="{x:Type ListViewItem}">
                    <Border x:Name="Bd" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" Padding="{TemplateBinding Padding}" SnapsToDevicePixels="true">
                        <ContentPresenter HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" VerticalAlignment="{TemplateBinding VerticalContentAlignment}"/>
                    </Border>
                    <ControlTemplate.Triggers>
                        <Trigger Property="IsEnabled" Value="false">
                            <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                        </Trigger>
                    </ControlTemplate.Triggers>
                </ControlTemplate>
            </Setter.Value>
        </Setter>
    

    【讨论】:

      【解决方案5】:

      将每个 ListViewItem 的样式设置为将 Focusable 设置为 false。

      <ListView ItemsSource="{Binding Test}" >
          <ListView.ItemContainerStyle>
              <Style TargetType="{x:Type ListViewItem}">
                  <Setter Property="Focusable" Value="False"/>
              </Style>
          </ListView.ItemContainerStyle>
      </ListView>
      

      【讨论】:

      • 简单高效。它实际上做了被问到的事情:它关闭了选择,而不是仅仅隐藏它。这是最好的答案。
      【解决方案6】:

      我找到的最简单的方法:

      <Setter Property="Focusable" Value="false"/>
      

      【讨论】:

      • @Mutex 它确实有效 - 你必须在 ListView.ItemContainerStyle 上应用它!
      【解决方案7】:

      列表视图的属性之一是IsHitTestVisible。 取消选中它。

      【讨论】:

      • 简单有效,也解决了我的 ScrollViewer 包裹在列表视图旁边的问题。
      • 我在 listview itemtemplate 中有按钮模板。如果取消选择IsHitTestVisible,则无法单击该按钮。它就像IsEnabled = false;
      【解决方案8】:

      好的,游戏有点晚了,但是这些解决方案都没有完全达到我想要做的。 这些解决方案有几个问题

      1. 禁用 ListViewItem,这会破坏样式并禁用所有子控件
      2. 从命中测试堆栈中删除,即子控件永远不会鼠标悬停或单击
      3. 让它无法聚焦,这对我不起作用?

      我想要一个带有分组标题的 ListView,每个 ListViewItem 应该只是“信息性”而不需要选择或悬停,但 ListViewItem 中有一个按钮,我希望它可以点击并悬停。

      所以,我真正想要的是 ListViewItem 根本不是 ListViewItem,所以,我过度使用了 ListViewItem 的 ControlTemplate,只是把它变成了一个简单的 ContentControl。

      <ListView.ItemContainerStyle>
          <Style TargetType="ListViewItem">
              <Setter Property="Template">
                  <Setter.Value>
                      <ControlTemplate>
                          <ContentControl Content="{Binding Content, RelativeSource={RelativeSource TemplatedParent}}"/>
                      </ControlTemplate>
                  </Setter.Value>
               </Setter>
           </Style>
      </ListView.ItemContainerStyle>
      

      【讨论】:

        【解决方案9】:

        这适用于可能遇到以下要求的其他人:

        1. 完全取代“已选择”的视觉指示(例如使用某种形状),而不仅仅是更改标准突出显示的颜色
        2. 在 DataTemplate 中包含这个选定的指示以及模型的其他可视化表示,但是,
        3. 不希望将“IsSelectedItem”属性添加到您的模型类,并且不希望在所有模型对象上手动操作该属性。
        4. 要求在 ListView 中可选择项目
        5. 也想替换 IsMouseOver 的可视化表示

        如果您像我一样(使用带有 .NET 4.5 的 WPF)并发现涉及样式触发器的解决方案根本不起作用,这是我的解决方案:

        替换样式中ListViewItem的ControlTemplate:

        <ListView ItemsSource="{Binding MyStrings}" ItemTemplate="{StaticResource dtStrings}">
                <ListView.ItemContainerStyle>
                    <Style TargetType="ListViewItem">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="ListViewItem">
                                    <ContentPresenter/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </ListView.ItemContainerStyle>
            </ListView>
        

        ..还有数据模板:

        <DataTemplate x:Key="dtStrings">
                <Border Background="LightCoral" Width="80" Height="24" Margin="1">
                    <Grid >
                        <Border Grid.ColumnSpan="2" Background="#88FF0000" Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsMouseOver, Converter={StaticResource conBoolToVisibilityTrueIsVisibleFalseIsCollapsed}}"/>
                        <Rectangle Grid.Column="0" Fill="Lime" Width="10" HorizontalAlignment="Left" Visibility="{Binding RelativeSource={RelativeSource AncestorType=ListViewItem}, Path=IsSelected, Converter={StaticResource conBoolToVisibilityTrueIsVisibleFalseIsCollapsed}}" />
                        <TextBlock Grid.Column="1" Text="{Binding}" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="White" />
                    </Grid>
                </Border>
            </DataTemplate>
        

        在运行时产生这个结果(选择了“B”项,“D”项有鼠标悬停):

        【讨论】:

          【解决方案10】:

          使用下面的代码:

          <ListView.ItemContainerStyle>
              <Style TargetType="{x:Type ListViewItem}">
                  <Setter Property="Background" Value="Transparent" />
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="{x:Type ListViewItem}">
                              <ContentPresenter />
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
          </ListView.ItemContainerStyle>
          

          【讨论】:

            【解决方案11】:

            以下代码禁用 ListViewItem 行选择,还允许添加填充、边距等。

            <ListView.ItemContainerStyle>                                                                              
               <Style TargetType="ListViewItem">                                                                                      
                   <Setter Property="Template">                                                                                            
                     <Setter.Value>                                                                                             
                       <ControlTemplate TargetType="{x:Type ListViewItem}">                                                                                                    
                          <ListViewItem Padding="0" Margin="0">                                                                                                        
                              <ContentPresenter />
                          </ListViewItem>
                       </ControlTemplate>                                                          
                     </Setter.Value>                                                                                       
                     </Setter>
                  </Style>                                                                      
              </ListView.ItemContainerStyle> 
            

            【讨论】:

              【解决方案12】:

              以下代码禁用对 ListViewItem 的关注

              <ListView.ItemContainerStyle>
              <Style TargetType="{x:Type ListViewItem}">
                  <Setter Property="Background" Value="Transparent" />
                  <Setter Property="Template">
                      <Setter.Value>
                          <ControlTemplate TargetType="{x:Type ListViewItem}">
                              <ContentPresenter />
                          </ControlTemplate>
                      </Setter.Value>
                  </Setter>
              </Style>
              

              【讨论】:

                【解决方案13】:
                        <ListView Grid.Row="1" ItemsSource="{Binding Properties}" >
                            <!--Disable selection of items-->
                            <ListView.Resources>
                                <Style TargetType="{x:Type ListViewItem}">
                                    <Setter Property="Background" Value="Transparent" />
                                    <Setter Property="BorderBrush" Value="Transparent"/>
                                    <Setter Property="VerticalContentAlignment" Value="Center"/>
                                    <Setter Property="Template">
                                        <Setter.Value>
                                            <ControlTemplate TargetType="{x:Type ListViewItem}">
                                                <Grid Background="{TemplateBinding Background}">
                                                    <Border Name="Selection" Visibility="Collapsed" />
                                                    <!-- This is used when GridView is put inside the ListView -->
                                                    <GridViewRowPresenter Grid.RowSpan="2"
                                                      Margin="{TemplateBinding Padding}"
                                                      HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                                                      VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                                                      SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                
                                                </Grid>
                                            </ControlTemplate>
                                        </Setter.Value>
                                    </Setter>
                                </Style>
                            </ListView.Resources>
                            <ListView.View>
                                <GridView>
                                    <GridViewColumn Width="90" DisplayMemberBinding="{Binding Name}"  />
                                    <GridViewColumn Width="90" CellTemplateSelector="{StaticResource customCellTemplateSelector}"  />
                                </GridView>
                            </ListView.View>
                        </ListView>
                

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2010-11-16
                  • 2019-01-17
                  • 2014-08-07
                  • 2016-03-12
                  • 2011-03-31
                  相关资源
                  最近更新 更多