我如何禁用列表框的选择?


当前回答

我发现了一个非常简单和直接的解决方法,我希望它也适用于你

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

其他回答

注意:这个解决方案不禁用选择键盘导航或右键单击(即。方向键后接空格键)

所有之前的答案要么完全删除技能选择(运行时没有切换),要么只是删除视觉效果,但不删除选择。

但是,如果您希望能够通过代码而不是通过用户输入来选择和显示选择,该怎么办呢?也许你想“冻结”用户的选择,而不是禁用整个列表框?

解决方案是将整个ItemsContentTemplate包装成一个没有视觉chrome的Button。按钮的大小必须等于Item的大小,所以它是完全覆盖的。 现在使用按钮的isenabled -属性:

启用按钮“冻结”项目的选择状态。这是可行的,因为启用按钮在鼠标事件冒泡到listboxitem - eventandler之前吃掉了所有的鼠标事件。你的ItemsDataTemplate仍然会收到MouseEvents,因为它是按钮内容的一部分。

禁用此按钮可通过单击更改选择。

<Style x:Key="LedCT" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <Button IsEnabled="{Binding IsSelectable, Converter={StaticResource BoolOppositeConverter}}" Template="{DynamicResource InvisibleButton}">
                        <ContentPresenter />
                </Button>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

<ControlTemplate x:Key="InvisibleButton" TargetType="{x:Type Button}">
    <ContentPresenter/>
</ControlTemplate>

达特拉克斯

对我来说,最好的解决办法是:

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

虽然@Drew Noakes的回答是大多数情况下的快速解决方案,但设置x:静态笔刷有一点缺陷。

当您按照建议设置x:Static笔刷时,列表框项中的所有子控件都将继承此样式。

这意味着,虽然这将用于禁用列表框项的高亮显示,但它可能会导致子控件产生不希望看到的效果。

例如,如果你在ListBoxItem中有一个组合框,它将禁用鼠标在组合框中高亮显示。

相反,考虑设置已选、未选和MouseOver事件的VisualStates,就像在这个stackoverflow线程中提到的解决方案中提到的那样:从ListBoxItem中删除控件高亮显示,但不删除子控件。

-弗林尼

这也将工作,如果我有需要使用列表框而不是itemscontrol,但只是显示不应该是可选的项目,我使用:

<ListBox.ItemContainerStyle>
    <Style TargetType="ListBoxItem">
        <Setter Property="IsHitTestVisible" Value="False" />
    </Style>
</ListBox.ItemContainerStyle>

一个简单的修复工作在Windows Phone上,例如在选择设置选定项为空:

    <ListBox SelectionChanged="ListBox_SelectionChanged">

在后面的代码中:

    private void ListBox_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
    {
        (sender as ListBox).SelectedItem = null;
    }