gpt4 book ai didi

wpf - WPF ListBox 可以是 "read only"吗?

转载 作者:行者123 更新时间:2023-12-03 21:28:04 26 4
gpt4 key购买 nike

我们有一个场景,我们想要显示一个项目列表并指示哪个是“当前”项目(使用小箭头标记或更改的背景颜色)。

ItemsControl 对我们没有好处,因为我们需要“SelectedItem”的上下文。但是,我们希望以编程方式移动选择并且不允许用户更改它。

有没有一种简单的方法可以使 ListBox 非交互式?我们可以通过故意吞下鼠标和键盘事件来捏造它,但是我是否缺少一些基本属性(例如将“IsEnabled”设置为 false 而不影响其视觉样式),这给了我们想要的东西?

或者......是否有另一个 WPF 控件是两全其美的 - 具有 SelectedItem 属性的 ItemsControl?

最佳答案

一种选择是设置 ListBoxItem.IsEnabledfalse :

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

这可确保项目不可选择,但它们可能无法按照您的喜好呈现。要解决此问题,您可以使用触发器和/或模板。例如:
<ListBox x:Name="_listBox">
<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>

关于wpf - WPF ListBox 可以是 "read only"吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/161127/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com