简单的WPF MVVM +绑定 [英] Simple WPF + MVVM binding

查看:631
本文介绍了简单的WPF MVVM +绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为类 mywindow的窗口中提炼出来。我使用MVVM模式,这样,代码隐藏我有以下字段:

I have a class named MyWindow the derives from Window. I use the MVVM pattern so in code-behind I have the following field:

public MyViewModel ViewModel = new MyViewModel();



视图模型包含<$ C的集合$ C>人,以及所有我想要做的是绑定一个组合框来此集合,展示人员。名称为每个

标头,我也想在<$ C $另一场C>视图模型,将数据绑定到选定的项目。

ViewModel contains a collection of Person, and all I'd like to do is to bind a ComboBox to this collection, show Person.Name as the header for each Person.
I would also like to have another field in ViewModel that will be data-bound to the selected item.

请帮我。

推荐答案

那么首先你必须设置你的窗口,在构造函数中viewmdodel的DataContext的,如果你还没有这样做的:

Well firstly you have to set the datacontext of your window to the viewmdodel in the constructor if you have not already done so:

this.DataContext = MyModelView;



然后就可以设置组合框,如下所示:

Then you can set the ComboBox as follows:

<ComboBox ItemsSource={Binding Persons} SelectedItem={Binding CurrentPerson,Mode=TwoWay} DisplayMemberPath="Name"/>



在哪里的人是人的收集和当前人是物业所选择的人将被绑定到。

Where Persons is the Collection of Persons and Current Person is the Property the selected person will be bound to.

这篇关于简单的WPF MVVM +绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆