MVVM Light和下拉框

4

我刚接触WPF和MVVM Light,希望您能帮助我 :-)

我想知道如何使用MVVM Light实现下拉框,并做到以下几点:

1)选择下拉框中的项目

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

谢谢您的帮助。

Romain


请发布您的视图模型类代码,以便我们知道哪些属性应该被绑定,哪些属性应该被更改。 - vortexwolf
1个回答

6

Well:

View:

<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帮助类,以更新视图中要更新的属性。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接