wpf 禁用选择或关注列表框项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23231334/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Disable Selection or Focus on Listbox Items
提问by user3342256
I have a WPF application which contains dynamically generated listbox items. 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:
我有一个 WPF 应用程序,其中包含动态生成的列表框项目。我想禁用列表框项目的选择或“可聚焦性”,而不禁用每个列表框项目中的所有控件(它们包含用户需要能够与之交互的按钮和链接)。下面是我试图阻止的图片:


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.
这张图片显示了两个列表框项目。我点击了顶部列表框项目的背景区域并选择了它,这使得文本更难阅读并且在视觉上没有吸引力。
回答by Jarod Kientz
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>
回答by Dmitry
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,其中包含可能具有其样式的特定项目容器。

