简体   繁体   English

禁用选择或关注列表框项目

[英]Disable Selection or Focus on Listbox Items

I have a WPF application which contains dynamically generated listbox items. 我有一个WPF应用程序,其中包含动态生成的列表框项目。 I would like to disable the selection or "focusability" of the listbox items without disabling all of the controls in each listbox item (they contain buttons and links users need to be able to interact with). 我想禁用列表框项目的选择或“可聚焦性”,而不禁用每个列表框项目中的所有控件(它们包含用户需要进行交互的按钮和链接)。 Below is a picture of what I'm trying to prevent: 以下是我要防止的照片:

在此处输入图片说明

This picture shows two listbox items. 此图显示了两个列表框项目。 I have clicked on the background area of the top listbox item and selected it, which makes text harder to read and is just visually unappealing. 我单击了顶部列表框项目的背景区域并选择了它,这使文本更难以阅读,并且在视觉上不那么吸引人。

You could set ListBoxItem.IsEnabled property to false like so: 您可以像这样将ListBoxItem.IsEnabled属性设置为false:

<ListBox x:Name="_sampleListBox">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsEnabled" Value="False"/>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

This would keep the items from being able to be selected. 这将使项目无法被选择。 It may look kinda funny though... You could try using templates like so: 不过,它看起来可能有点可笑...您可以尝试使用如下模板:

<ListBox x:Name="_sampleListBox">
    <ListBox.ItemContainerStyle>
        <Style TargetType="ListBoxItem">
            <Setter Property="IsEnabled" Value="False"/>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Foreground" Value="Red" />
                </Trigger>
            </Style.Triggers>
        </Style>
    </ListBox.ItemContainerStyle>
</ListBox>

You can disable visual effects of the selection by using this ListBox: 您可以使用以下ListBox禁用所选内容的视觉效果:

<Style x:Key="NoSelectionListBoxItemStyle" TargetType="{x:Type ListBoxItem}">
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="HorizontalContentAlignment" Value="{Binding HorizontalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="VerticalContentAlignment" Value="{Binding VerticalContentAlignment, RelativeSource={RelativeSource AncestorType={x:Type ItemsControl}}}"/>
    <Setter Property="Padding" Value="2,0,0,0"/>
    <Setter Property="FocusVisualStyle">
        <Setter.Value>
            <Style>
                <!-- This removes focus visualization -->
                <Setter Property="Control.Template" Value="{x:Null}"/>
            </Style>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type ListBoxItem}">
                <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>
                                            <!-- Some default triggers removed to avoid background changes on selection -->
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Perhaps a cleaner solution would be to create your own ItemsControl with specific item containers that could have their style. 也许更干净的解决方案是使用可能具有其样式的特定项目容器创建自己的ItemsControl。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM