w3hello.com logo
Home PHP C# C++ Android Java Javascript Python IOS SQL HTML videos Categories
How to disable a ComboBoxItem that is provided via ObjectProvider Enum WPF C# in MVVM?

The data context that your Style binding is using is the data context of the ComboboxItem (this will be the enum element itself) not the view model, as you are hoping.

Try using a relative source on your binding to walk up the logical tree and get the data context of the ComboBox, not the ComboBoxItem.

<Setter Property="IsEnabled" Value="{Binding
DataContext.CanDisplayDetails, RelativeSource={RelativeSource FindAncestor,
AncestorType={x:Type ComboBox}}}"/>

You can diagnose this yourself by checking the visual studio output window. You will see a binding error telling you something like;

Data Error : CanDisplayDetails property cannot be found on a _InvoiceActionsLong_ instance


In order to have this apply to only your View item consider;

  • a custom StyleSelector that will only apply this style is the content of the ComboBoxItem is View. Set the ComboBox.ItemContainerStyleSelector property to an instance of your selector.
  • an IMultiValueConverter implementation for the binding to CanDisplayDetails and the enum value. This converter would always return true if the value is not View.

Why does this happen?

The data context of generated ItemSelector items like ComboBoxItem is automatically set to be the data that the item represents.

© Copyright 2018 w3hello.com Publishing Limited. All rights reserved.