Night Walker
Night Walker

Reputation: 21260

combobox databinding to selectedvalue event

I want to execute some function when user selects a different value in the combo box.

I am using MVVM pattern .

Any Idea how I Data Bind in this case ?

Upvotes: 0

Views: 280

Answers (2)

myermian
myermian

Reputation: 32505

You'd want to either bind to the SelectedValue, SelectedItem, or SelectedIndex depending on what you want to do:

public class MyViewModel : INotifyPropertyChanged
{
    public ObservableCollection<MyObj> MyCollection { get; private set; }

    private MyObj _theItem;
    public MyObj TheItem
    {
        get { return _theItem; }
        set
        {
            if (Equals(value, _theItem)) return;

            _theItem= value;

            //Or however else you implement this...
            OnPropertyChanged("TheItem");
            //Do something here.... OR
            //This will trigger a PropertyChangedEvent if you're subscribed internally.
        }
    }

    private string _theValue;
    public string TheValue
    {
        get { return _theValue; }
        set
        {
            if (Equals(value, _theValue)) return;

            _theValue= value;
            OnPropertyChanged("TheValue");
        }
    }

    private int _theIndex;
    public int TheIndex
    {
        get { return _theIndex; }
        set
        {
            if (Equals(value, _theIndex)) return;

            _theIndex = value;
            OnPropertyChanged("TheIndex");
        }
    }
}

public class MyObj
{
    public string PropA { get; set; }
}

 <!-- Realistically, you'd only want to bind to one of those -->
 <!-- Most likely the SelectedItem for the best usage -->
 <ItemsControl ItemsSource="{Binding Path=MyCollection}"
               SelectedItem="{Binding Path=TheItem}"
               SelectedValue="{Binding Path=TheValue}"
               SelectedIndex="{Binding Path=TheIndex}"
               />

Upvotes: 1

bebonham
bebonham

Reputation: 135

bind to a property on SelectedItem. in the property's setter, execute your function. SelectedItem="{Binding Path=SomeProperty}"

Upvotes: 2

Related Questions