WPF 使用 MVVM 禁用列表框项目

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13769607/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 06:32:02  来源:igfitidea点击:

WPF Disable listbox items with MVVM

c#wpfmvvmlistboxdatatrigger

提问by user1886304

I tried searching through a lot of threads and I've found several that's pretty close but not exactly what I want. After a day of searching, I just decided to ask. Apologies if I missed something, I feel like this would be common enough but I can't seem to quite get it.

我尝试搜索了很多线程,我发现了几个非常接近但不完全是我想要的。经过一天的搜索,我决定问一下。抱歉,如果我错过了什么,我觉得这很常见,但我似乎不太明白。

I have UserControl that bounded to a ViewModel, and it has a Listbox with ItemsSource=ObservableCollection which is a property of the ViewModel like below:

我有一个绑定到 ViewModel 的 UserControl,它有一个 Listbox,其中 ItemsSource=ObservableCollection 是 ViewModel 的一个属性,如下所示:

Whenever I select an item, I call SomeObject.IsEnabled = false on several other items depending on certain conditions. I'd like to bind the listbox items to that IsEnabled property so I can grayout whichever items whenever I do a selection.

每当我选择一个项目时,我都会根据某些条件在其他几个项目上调用 SomeObject.IsEnabled = false 。我想将列表框项目绑定到该 IsEnabled 属性,这样我就可以在进行选择时将任何项目灰显。

ViewModel:

视图模型:

Class ViewModel : PropertyNotificationObject
{
    private ObservableCollection<SomeObject> m_list;
    public ObservableCollection<SomeObject> List {get; set;} //notifying properly

    private void selectedItem()
    {
        //several in SomeObjects in List sets IsEnabled = false
    }
} 

The Object Class

项目等级

class SomeObject : PropertyNotificationObject
{
   private bool m_isEnabled;
   public IsEnabled { get; set; } //notifying properly
}

XAML

XAML

 <DataTemplate x:Key="ListTemplate">
    <Grid>
       <Grid.ColumnDefinitions>
          <ColumnDefinition Width="*"/>
       </Grid.ColumnDefinitions>
       <TextBlock Text="{Binding ., Converter={someConverterObjectToString}}"/>
    </Grid>
 </DataTemplate>
 <ListBox ItemsSource="{Binding List}" ItemTemplate="{StaticResource ListTemplate}"/>

I've tried using StyleTriggers in the ListBox.ItemContainerStyle and the DataTemplate like below but I couldn't figure out how to get to the SomeOject.IsEnabled property.

我已经尝试在 ListBox.ItemContainerStyle 和 DataTemplate 中使用 StyleTriggers,如下所示,但我不知道如何获得 SomeOject.IsEnabled 属性。

<ListBox.ItemContainerStyle>
    <Style TargetType="{x:Type ListBoxItem}">
        <Style.Triggers>
            <DataTrigger Binding={???????? I can't get to my SomeObject properties.}
        </Style.Triggers>
    </Style>
</ListBox.ItemContainerStyle>

Sorry for the lack of colors, I'm new here and don't really know how use the editor very well.

抱歉缺少颜色,我是新来的,不太了解如何很好地使用编辑器。

Thanks in advance.

提前致谢。

回答by Federico Berasategui

{Binding IsEnabled}in your ItemContainerStyleshould do the work. Look at VS debug window for binding errors

{Binding IsEnabled}在你ItemContainerStyle应该做的工作。查看 VS 调试窗口是否存在绑定错误

Editor directly bind the IsEnabled property of the ListBoxItem:

编辑或直接绑定 ListBoxItem 的 IsEnabled 属性:

<Style TargetType="{x:Type ListBoxItem}">
    <Setter Property="IsEnabled" Value="{Binding IsEnabled}"/>
</Style>