2013-01-02 40 views
4

我是新来WPF和MVVM光,我将不胜感激,如果你能帮助我:-)MVVM灯和ComboBox

我想知道如何实现与MVVM光一个ComboBox做到以下几点:

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

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

谢谢你的帮助。

罗曼

+0

使用视图模型类发布代码,以便我们知道应绑定哪些属性以及应更改哪些属性。 – vorrtex

回答

6

好:

查看:

<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帮助器类。

+0

非常感谢:-)这正是我所期待的。 – Romain