首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MVVM Light和combobox

MVVM Light和combobox
EN

Stack Overflow用户
提问于 2013-01-02 14:58:46
回答 1查看 2.5K关注 0票数 4

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

我想知道如何使用MVVM Light实现combobox来做以下事情:

1)在组合框中选择一项

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

谢谢你的帮助。

罗曼

EN

回答 1

Stack Overflow用户

发布于 2013-01-02 22:34:06

好的:

查看:

代码语言:javascript
复制
<ComboBox ItemsSource="{Binding SourceData}" SelectedItem="{Binding SelectedSourceData,Mode=TwoWay}"/>
<TextBlock Text="{Binding SelectedDataInTextFormat}"/>

ViewModel:

代码语言:javascript
复制
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帮助器类。

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14118062

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档