【问题标题】:make Listbox items in WPF not selectable使 WPF 中的列表框项目不可选
【发布时间】:2010-12-15 21:35:33
【问题描述】:

我在 WPF 中有一个列表框,当他们选择一个项目时,它显示出难看的颜色 我可以将所有项目都设为不可选择吗?

【问题讨论】:

标签: wpf listbox


【解决方案1】:

如果您不需要选择,请使用ItemsControl 而不是ListBox

【讨论】:

  • 并非总是如此; ItemsControl 不能做一些 ListBox 可能需要的事情,例如在使用虚拟化时 ScrollIntoView
  • 不一定正确。不想使用列表框的 original 选择机制但仍保留其功能可能有很多原因:仅举一个例子,考虑一个图像列表框,您希望在其中添加一个额外的复选框每个图像的角落以启用选择。您可以将此复选框连接到原始选择机制,但您仍要禁用 ListBox 的原始单击选择。
【解决方案2】:

在 ListBoxItem 样式中将 Focusable 属性添加为 false:

<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}">
  <!-- Possibly other setters -->
  <Setter Property="Focusable" Value="False" />
</Style>

【讨论】:

  • 这才是真正的答案。
【解决方案3】:

请在您的列表框中使用它。我发现了这个非常优雅的解决方案

<ListBox ItemsSource="{Binding YourCollection}">
    <ListBox.ItemContainerStyle>
       <Style TargetType="{x:Type ListBoxItem}">
           <Setter Property="Focusable" Value="False"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

【讨论】:

  • 确实是最优雅的解决方案
【解决方案4】:

如果您不希望它们可选择,那么您可能不想要列表视图。 但如果这是你真正需要的,那么你可以用一种风格来做到这一点:

<Page
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
  <Page.Resources>


<Style x:Key="{x:Type ListBoxItem}" TargetType="{x:Type ListBoxItem}">
  <Setter Property="Template">
    <Setter.Value>
      <ControlTemplate TargetType="{x:Type ListBoxItem}">
        <Border 
          Name="Border"
          Padding="2"
          SnapsToDevicePixels="true">
          <ContentPresenter />
        </Border>
        <ControlTemplate.Triggers>
          <Trigger Property="IsSelected" Value="true">
            <Setter TargetName="Border" Property="Background" Value="#DDDDDD"/>
          </Trigger>
          <Trigger Property="IsEnabled" Value="false">
            <Setter Property="Foreground" Value="#888888"/>
          </Trigger>
        </ControlTemplate.Triggers>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
</Style>

  </Page.Resources>
  <Grid>  
    <ListBox>
      <ListBoxItem>One</ListBoxItem>
      <ListBoxItem>Two</ListBoxItem>
      <ListBoxItem>Three</ListBoxItem>
    </ListBox>
  </Grid>
</Page>

查看 IsSelected 触发器。您可以将边框设置为不同的颜色,使其不“丑”或将其设置为透明,并且在选中时不可见。

希望这会有所帮助。

【讨论】:

  • 添加 以摆脱焦点矩形。
【解决方案5】:

还有一个更简单的方法:设置ListBox 属性IsHitTestVisible="False"。这可以防止列表中的所有项目接收鼠标事件。这具有在鼠标悬停时停止突出显示的优点。

它在 WP 7.1 中适用于我。

【讨论】:

  • 但是整个列表框没有响应..包括滚动条。
  • @EladKatz:这是真的。这就是为什么我有时会在 ListBox 周围添加自己的 ScrollViewer 以重新建立滚动。
  • @DeniseDraper 这是个好主意,但是当列表已满时,我的滚动查看器无法“滚动”。我知道内容足以让滚动条工作,但它只是保持“不活动” “..有什么想法吗?
  • @StinkyCat:这里没有想法,抱歉。不过,滚动发生的奇怪事情是 StackOverflow 上的一个常见问题主题,所以我怀疑它与这种情况有任何特定联系。
  • 到目前为止,最简单、最直接的解决方案是呈现一个列表框,其中包含需要变得不可选择的项目。
【解决方案6】:

执行此操作的一种简单方法(使用上面 viky 的答案)是在 SelectionChanged() 中将所选索引设置为 -1,如下所示。

public void OnListView_SelectionChanged(Object sender, RoutedEventArgs e)
{
    if (null != sender && sender is ListView)
    {
        ListView lv = sender as ListView;
        lv.SelectedIndex = -1;
    }
}

【讨论】:

    【解决方案7】:

    最好避免事件,Style标签更优雅且无副作用。

    <ListBox>
      <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
          <Setter Property="IsEnabled" Value="False"/>
        </Style>
      </ListBox.ItemContainerStyle>
      <ListBox.ItemTemplate>
        <DataTemplate>
          <StackPanel>
            ... what you want as a source ...
           </StackPanel>
        </DataTemplate>
      </ListBox.ItemTemplate>
    </ListBox>
    

    【讨论】:

      【解决方案8】:

      您可以处理 ListBox 的 SelectionChanged 事件,并在事件处理程序中取消选择所选项目。

      【讨论】:

        【解决方案9】:

        您还可以禁用列表框,这将为您提供静态、非交互式列表框。

        <ListBox IsEnabled="False"/>
        

        我认为这是尽可能简单的解决方案。

        【讨论】:

          【解决方案10】:

          在我的例子中,我使用一个文本块和一个组合框来模板化 ListboxItems。唯一的“活跃”应该是 Combo...

          <ScrollViewer VerticalScrollBarVisibility="Auto"
                        HorizontalScrollBarVisibility="Disabled"
                        CanContentScroll="True" />
              <ItemsControl>
               ....here my content....
              </Itemscontrol>
          </ScrollViewer>
          

          确实按预期为我工作。 BR, 丹尼尔

          【讨论】:

            【解决方案11】:

            你也可以处理 PreviewMouseDown 事件

            为了防止点击,您可以设置KeyboardNavigation.TabNavigation="None"

            <ListView x:Name="Cards"
                        .....
                        PreviewMouseDown="CardMonthsDescriptors_OnPreviewMouseDown"
                        KeyboardNavigation.TabNavigation="None"
            >
            ...
            
            private void Cards_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
                    {
                        e.Handled = true;
                    }
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2018-12-09
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2015-10-20
              相关资源
              最近更新 更多