2013-02-27 75 views
1

我无法使用MVVM方法在XAML中使用DataBinding组合。Combo未使用MVVM和XAML更新

我设置了组合的代码源的后面,像这样:

AgesViewModel agesViewModel = new AgesViewModel(); 
comboAge.SelectedValuePath = "AgeID"; 
comboAge.DisplayMemberPath = "Age"; 
comboAge.ItemsSource = agesViewModel.GetAges(); 

而XAML的组合是:

<ComboBox x:Name="comboAge" 
      SelectedValue="{Binding AgeID}" /> 

的组合与文本控件和他们网页的部分内容所有的DataBind都很好。 DataContext页面被设置为ViewModel(carViewModel),它具有一个名为AgeID的属性。

所以,当组合项目更改时,我想carViewModel.AgeID更新为选定的值。

每当我从组合中选择一个项目什么都得不到更新。我究竟做错了什么??

在此先感谢

我使用XAML,C#4.5,写在Visual Studio中的Windows应用商店的应用程序2012

回答

2

如果你想跟着MVVM模式,具有组合框的数据也可在你的carViewModel绑定它。

换句话说:

<ComboBox x:Name="comboAge" ItemsSource="{Binding MyAgeList}" SelectedValue="{Binding AgeID}" /> 

当改变ComboBox中值,这也将更新AgeID属性。

假设AgeID为int(它可以,当然,你想要什么)然后MyAgeList应该像这样carViewModel定义:

public List<int> MyAgeList {get; set;} 

// Constructor 
public CarViewModel() 
{ 
    MyAgeList = new AgesViewModel().GetAges(); 
} 
+0

OK,我已经做了,但我有同样的结果。现在我来的XAML如下所示: Sun 2013-02-27 14:05:38

+0

而我的CarViewModel现在是这样的: public CarViewModel() AgeList = new AgesViewModel()。GetAges(); } public ObservableCollection AgeList {get;组; } – Sun 2013-02-27 14:09:13

+0

为什么AgeList是ViewModels的集合? – Blachshma 2013-02-27 14:53:40