2013-02-27 33 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