【问题标题】:MVVM Light and comboboxMVVM 灯光和组合框
【发布时间】:2013-01-02 06:58:46
【问题描述】:

我是 WPF 和 MVVM Light 的新手,如果您能帮助我,我将不胜感激 :-)

我想知道如何使用 MVVM Light 实现一个组合框来执行以下操作:

1) 在组合框中选择一个项目

2) 根据选择的值,更改 GUI 中的其他文本字段。

感谢您的帮助。

罗曼

【问题讨论】:

  • 在您的视图模型类中发布代码,以便我们知道应该绑定哪些属性以及应该更改哪些属性。

标签: wpf combobox mvvm-light


【解决方案1】:

嗯:

查看:

<ComboBox ItemsSource="{Binding SourceData}" SelectedItem="{Binding SelectedSourceData,Mode=TwoWay}"/>
<TextBlock Text="{Binding SelectedDataInTextFormat}"/>

视图模型:

public class ViewModel:ViewModelBase
{
    public ObservableCollection<Foo> SourceData{get;set;}
    public Foo SelectedSourceData 
    { 
        get{return _selectedFoo;}
        set{_selectedFoo=value;
            RaisePropertyChanged("SelectedSourceData");
            SelectedDataInTextFormat=Foo.ToString();
    }

    public string SelectedDataInTextFormat
    {
        get{return _selectedDataInTextFormat;}
        set{_selectedDataInTextFormat=value;
            RaisePropertyChanged("SelectedDataInTextFormat");
    }
}

基本上,为确保您的视图模型能够从组合框中接收更新后的选定项,请确保将 SelectedItem 绑定设置为 Mode=TwoWay。为了确保在视图模型中发生更改时将数据从视图模型推送到视图,请确保为要在视图中更新的属性调用 RaisePropertyChanged 帮助器类。

【讨论】:

  • 非常感谢 :-) 这正是我想要的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-04-11
  • 1970-01-01
  • 2011-07-23
  • 2012-07-22
相关资源
最近更新 更多