Question

my problem is that I want to call the 'SelectionChanged' Event in my ViewModel.

I have a ComboBox (here called ListPicker, it's a phone application):

<tool:ListPicker Name="txt_LZZ" 
    ItemsSource="{Binding ZZR}" SelectedItem="{Binding MySelectedItem}" />

My Property in the ViewModel looks like this:

    private List<string> _zzr;
    public List<string> ZZR
    {
        get
        {
            _zzr = new List<string>();
            _zzr.Add("Jahr");
            _zzr.Add("Monat");
            _zzr.Add("Woche");
            _zzr.Add("Tag");
            return _zzr;
        }
        set
        {
            _zzr = value;
            RaisePropertyChanged(() => ZZR);
        }
    }

    private string _mySelectedItem;
    public string MySelectedItem
    {
        get
        {
            return _mySelectedItem;
        }
        set
        {
            if (value == _mySelectedItem)
                return;
            _mySelectedItem = value;
            RaisePropertyChanged(() => MySelectedItem);
            GetValues();
        }
    }

The program only calls the get method once, while _mySelectedItem has the value 'null'. What I want is that when I change the SelectedItem in my Combobox (ListPicker), the ViewModel has to call the method GetValues, which is in my setter for MySelectedItem. Problem: -> ViewModel doesn't call setter. Why?

Was it helpful?

Solution

Try setting the mode of the binding to TwoWay:

<tool:ListPicker Name="txt_LZZ" 
    ItemsSource="{Binding ZZR}" SelectedItem="{Binding MySelectedItem, Mode=TwoWay}" />

I also wouldn't instantiate your value collection in the getter, but instead make the setter private, and assign a value to the property in your view model (e.g. the constructor or when the view model is activated).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top