user253105
user253105

Reputation: 89

How to enable/disable comboBox items for different comboBoxes in wpf by click in one using MVVM pattern?

I create a project using MVVM pattern. In my View I create comboBoxes. In ViewModel I create ObservableCollection for ItemsSource for comboBoxes with string values:

public ObservableCollection<string> ComboBoxItems
    {
        get; set;
    }

In ViewModel constructor I create list of Models (foreach comboBox). My Model class have only two properties: SelectedComboBoxItem and IsEnabledComboBoxItem.

I want to have a logic like if I select one item in one comboBox it shoud be disable in this comboBox and in all others. How could I do this with Binding? Now my xaml code look like this, but it disabled only selected comboBox item and only in one comboBox, where he was called from:

<ComboBox  ItemsSource="{Binding ComboBoxItems}"
           SelectedItem="{Binding SelectedComboBoxItem }" IsEditable="True">
      <ComboBox.ItemContainerStyle>
             <Style TargetType="ComboBoxItem">
                  <Setter Property="IsEnabled" Value="{Binding IsEnabledComboBoxItem}" />
             </Style>
      </ComboBox.ItemContainerStyle>
</ComboBox>

Upvotes: 0

Views: 955

Answers (1)

user253105
user253105

Reputation: 89

To enable/disable comboBox items i used converter for them in xaml, where passed all selected values from all comboBoxes (collection in ViewModel). In my converter class I did the check I needed and return a bool value(is item in collection in ViewModel or not). Maybe this helps somebody.

Upvotes: 0

Related Questions