0

I have a window with a combobox. This comboboxhas 5 ComboboxItems.

In the example I want that it is not possible to select the items 3, 4 and 5.

I've tried two different ways: MVVM way and codebehind way

MVVM way:

xaml:

<ComboBox SelectedIndex="{Binding Path=SaveIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=SaveSelectedItemCheck}" Name="SaveCombobox">

viewmodel:

public object SaveSelectedItemCheck
{
    get { return _control.SaveCombobox.Items[CurrentSaveIndex]; }
    set
    {
        if (value != _control.SaveCombobox.Items[0] && value != _control.SaveCombobox.Items[1])
        {
            OnPropertyChanged("SaveSelectedItemCheck");
        }
    }
}

codebehind way:

xaml:

<ComboBox SelectedIndex="{Binding Path=SaveIndex, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" SelectionChanged="Save_SelectionChanged">

codebehind:

private void Save_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    ComboBox combobox = sender as ComboBox;
    if(combobox == null)
    {
        return;
    }

    if (combobox.SelectedItem != combobox.Items[0] && combobox.SelectedItem != combobox.Items[1])
    {
        combobox.SelectedItem = combobox.Items[1];
        e.Handled = true;
    }
}

But it only works with the codebehind way, which is dirty.

Why doesn't work the MVVM way?

1
  • why do you bind on selectedindex AND selecteditem. Just use SelectedItem. Another way would be to bind your itemssource to a CollectionView. The collectionView will contain a propertry Selected so you won t have to bind to selecteditem
    – Florian
    Aug 10, 2012 at 13:43

3 Answers 3

1

As others said, you do not actually set any value in the property setter.

But more important IMO, I think you've misunderstood the MVVM key concepts. There are lots of issues with your ViewModel code:

public object SaveSelectedItemCheck
{
    get { return _control.SaveCombobox.Items[CurrentSaveIndex]; }
    set
    {
        if (value != _control.SaveCombobox.Items[0] && value != _control.SaveCombobox.Items[1])
        {
            OnPropertyChanged("SaveSelectedItemCheck");
        }
    }
}

You're referring to _control.SaveCombobox.Items, which are UI concept/objects. This isn't the goal of the ViewModel. And you're returning an object, you should strongly type your model!

What you should have is the following:

  • a model (strongly typed POCO classes)
  • ViewModels that do not deal with the view controls in any way (you could even separate views and ViewModels into different assemblies to ensure you're following this rule)
  • Views, with binding to ItemsSource for control such as Combobox

Model:

public class SomeObject : INotifyPropertyChanged
{
    private string someProperty;
    public string SomeProperty
    {
        get { return this.someProperty; }
        set
        {
            if (this.someProperty != value)
            {
                this.someProperty = value;
                OnPropertyChanged("SomeProperty");
            }
        }
    }
    ...
}

ViewModel:

public class ViewModel : SomeViewModelBase
{
    private ObservableCollection<SomeObject> items;
    private SomeObject selectedItem;

    public ObservableCollection<SomeObject> Items
    {
        get
        {
            return items;
        }
        set
        {
            if (this.items != value)
            {
                this.items = value;
                OnPropertyChanged("Items");
            }
        }
    }

    public ObservableCollection<SomeObject> SelectedItem
    {
        get
        {
            return selectedItem;
        }
        set
        {
            if (this.selectedItem != value)
            {
                this.selectedItem = value;
                OnPropertyChanged("SelectedItem");
            }
        }
    }

    ...

    // Anywhere in your view model:
    this.Items = new ObservableCollection<SomeObject>(...);
    this.SelectedItem = this.Items[2];
    // Etc.
}

View:

<ComboBox 
    ItemsSource={Binding Items}
    SelectedItem="{Binding SelectedItem, Mode=TwoWay}">

View code-behind:

Nothing for your example

1

Your ViewModel method doesn't set the value of the property - regardless of whether the value is valid or not. It just fires an event based on whether the value is valid.

In fact, on closer inspection you appear to have misunderstood the MVVM pattern somewhat, as it appears that your ViewModel code might be referring directly to the control it is supporting. You should have a backing field for your property as per a "normal" property.

More importantly, you should throw the PropertyChanged event whether the value is valid or not, because if the value has been overriden by the viewmodel then PropertyChanged will notify the UI that the combobox value needs to be re-set to a valid value.

0

You don't store any value in the setter in your MVVM way.

1
  • +1, The posted code doesn't change the selected index either. Aug 10, 2012 at 13:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.