4

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

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

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

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

谢谢您的帮助。

罗曼

4

1 回答 1

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

于 2013-01-02T14:34:06.267 回答