привязка комбинированного списка к выбранному событию - PullRequest
0 голосов
/ 17 декабря 2011

Я хочу выполнить некоторую функцию, когда пользователь выбирает другое значение в поле со списком.

Я использую шаблон MVVM.

Есть идеи, как связать данные в этом случае?

Ответы [ 2 ]

2 голосов
/ 17 декабря 2011

привязка к свойству на SelectedItem. в установщике свойства выполните свою функцию. SelectedItem = "{Binding Path = SomeProperty}"

1 голос
/ 17 декабря 2011

Вы хотите связать с SelectedValue, SelectedItem или SelectedIndex в зависимости от того, что вы хотите сделать:

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}"
               />
...