c# - 禁用选择或聚焦列表框项目

标签 c# wpf listbox

我有一个 WPF 应用程序,其中包含动态生成的列表框项目。我想禁用列表框项目的选择或“可聚焦性”,而不禁用每个列表框项目中的所有控件(它们包含用户需要能够交互的按钮和链接)。下面是我试图阻止的图片:

enter image description here

这张图片显示了两个列表框项目。我单击了顶部列表框项目的背景区域并选择了它,这使得文本难以阅读并且在视觉上没有吸引力。

最佳答案

您可以将 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>

关于c# - 禁用选择或聚焦列表框项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23231334/

相关文章:

c# - 为 swagger 的 "Try it out"指定示例请求

c# - 如何在我的程序的开始菜单中创建一个菜单?

c# - 有什么方法可以在 WPF 中 block 复制部分图片?

c# - 当 SelectionMode 设置为 Cell 时,如何突出显示 WPF DataGrid 中的一行

wpf - 如何在 WPF 中设置网格列定义的样式

image - 从隔离存储延迟加载列表框图像

wpf - MVVM - ListBox SelectedItem 绑定(bind)属性变为空

c# - 如何统一使用 JsonUtility 获取子数组

c# - 如何在 XAML/WPF ListBox Drop 事件中绑定(bind)命令

C# 排序列表同时还返回原始索引位置?