【问题标题】:MVVM SelectionChanged Combobox doesn't call 'Set'MVVM SelectionChanged 组合框不调用“设置”
【发布时间】:2013-04-03 11:12:34
【问题描述】:

我的问题是我想在 ViewModel 中调用“SelectionChanged”事件。

我有一个 ComboBox(这里称为 ListPicker,它是一个电话应用程序):

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

ViewModel 中的我的属性如下所示:

    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();
        }
    }

程序只调用一次get 方法,而_mySelectedItem 的值为'null'。我想要的是,当我在我的 Combobox (ListPicker) 中更改 SelectedItem 时,ViewModel 必须调用 GetValues 方法,该方法位于 MySelectedItem 的设置器中。问题:-> ViewModel 不调用 setter。为什么?

【问题讨论】:

    标签: mvvm combobox viewmodel selectionchanged


    【解决方案1】:

    尝试将绑定模式设置为TwoWay

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

    我也不会在 getter 中实例化您的值集合,而是将 setter 设为私有,并为您的视图模型中的属性分配一个值(例如构造函数或激活视图模型时)。

    【讨论】:

    • 我已经设置了 ItemsSource,因为我认为这没有必要并且我不想专注于此...我将编辑我的第一篇文章。
    • 谢谢! :) 问题不在于设置“Mode=TwoWay”。是的,我知道我不应该在 getter 中设置值,但这只是为了测试目的。
    猜你喜欢
    • 2018-05-03
    • 2016-10-06
    • 1970-01-01
    • 2012-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多