仅通过用户界面禁用列表框选择

本文关键字:列表 选择 用户界面 | 更新日期: 2023-09-27 17:59:25

我有一个ListBox,它绑定到一个自定义类:

public class SelectionModel : BaseNotifyProperyChanged
{
    private bool _selected;
    public bool IsSelected
    {
         get { return _selected; }
         set
         {
             _selected = value;
             base.RaisePropertyChanged( "IsSelected" );
         }
    }
    //.... 
}

通过在xaml:中的实现

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}"/>
</Style>

我只想禁用用户界面中的选择,保持数据绑定不变,这可能吗?我知道我可以通过使用样式禁用ListBoxItems来禁用选择,但这会使绑定无效。

编辑:这似乎有些混乱,所以我来澄清一下。我不想删除选择或突出显示,我希望能够以编程方式选择项目,但不能通过用户界面所以禁用物品或更改高亮颜色不是我想要的。

仅通过用户界面禁用列表框选择

你不能禁用它吗?

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=OneWay}"/>
    <Setter Property="IsEnabled" Value="False"/>
</Style>