C# 禁用对列表框项目的选择或聚焦
我有一个WPF应用程序,它包含动态生成的listbox项。我想禁用列表框项目的选择或“聚焦”,而不禁用每个列表框项目中的所有控件(它们包含用户需要能够交互的按钮和链接)。下面是我试图阻止的情况:C# 禁用对列表框项目的选择或聚焦,c#,wpf,listbox,C#,Wpf,Listbox,我有一个WPF应用程序,它包含动态生成的listbox项。我想禁用列表框项目的选择或“聚焦”,而不禁用每个列表框项目中的所有控件(它们包含用户需要能够交互的按钮和链接)。下面是我试图阻止的情况: 此图显示了两个列表框项目。我已单击顶部列表框项目的背景区域并选中它,这会使文本更难阅读,而且在视觉上没有吸引力。您可以将ListBoxItem.IsEnabled属性设置为false,如下所示: <ListBox x:Name="_sampleListBox"> <ListB
此图显示了两个列表框项目。我已单击顶部列表框项目的背景区域并选中它,这会使文本更难阅读,而且在视觉上没有吸引力。您可以将ListBoxItem.IsEnabled属性设置为false,如下所示:
<ListBox x:Name="_sampleListBox">
<ListBox.ItemContainerStyle>
<Style TargetType="ListBoxItem">
<Setter Property="IsEnabled" Value="False"/>
</Style>
</ListBox.ItemContainerStyle>
</ListBox>
这将使项目无法被选择。虽然看起来有点滑稽。。。您可以尝试使用如下模板:
<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>
您可以使用此列表框禁用所选内容的视觉效果:
<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>
也许一个更简洁的解决方案是使用特定的项目容器创建自己的项目控件,这些容器可以有自己的样式。如果不想选择,那么您可以使用一个简单的项目控件并在其样式中添加一个ScrollViewer。
动态生成的列表框项目
听起来很奇怪,也太winforms了。你应该使用数据绑定,是的,@Didier说你需要一个ItemsControl
而不是ListBox
。我可能使用了错误的术语。listbox项是通过可观察集合上的数据绑定显示的。我实现了ItemsControl而不是listbox,它确实禁用了选择功能,但它似乎有一个缺点。使用ItemsControl,我有一个
XAML部分,用于在鼠标悬停事件时控制单个列表框项目的边框、突出显示等。使用ItemsControl,样式将应用于整个列表,而不仅仅是单个项目。我尝试使用
重新编写我的样式,但这并不存在。选项?简单ItemsControl的项被包装到ContentPresenter中。不幸的是,它没有模板属性。这不适合我的需要。请参阅我的问题陈述中的第二句。不过对我来说非常适合:)+1我已将此样式块完全按照编写的方式添加到我的XAML中,并且它编译和运行良好,但我的listbox项的选择行为没有改变。您是否将其设置为listbox的ItemsContainerStyle?