2013-06-02 59 views
0

我有填充有值從一個表,並且保持從的ObservableCollection(不同的表)填充一個ComboBox一個TemplateColumn中一個數據網格。這肯定不是最優雅的方式來做到這一點,但因爲我沒有時間從零開始,並開始使用MVVM方法...從組合框WPF數據網格通值的SelectionChanged

比方說: 表狗 & 表Dog_breeds

private void breed_combo_SelectionChanged(object sender, SelectionChangedEventArgs e) 
    { 
    ComboBox comboBox = (ComboBox) sender; 
    string test = comboBox.SelectedValue.ToString(); 
    //parse the value as int and somehow pass to the according row 

    } 

網格時,選擇在ComboBox因此改變從表狗和ComboBox中Dog_breeds

我需要改變在狗表id_dog_breed持有價值

我該如何做到這一點?我相信在很久以前的某個地方一定會有類似的問題,但我沒有找到答案。

回答

0

不將其轉換爲字符串,投在組合框中選擇的項目到它的ItemSource綁定到一個對象類型,那麼你應該得到它的所有屬性

private void breed_combo_SelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    ComboBox comboBox = (ComboBox) sender; 
    var selectedBreed = (Dog_breed)comboBox.SelectedValue; 
    //now you can access the id and pass it on 
    //assuming that the Id is property-> selectedBreed.Id 
    //you should be able to access Dogs through items, I am guessing here what you structure is 
    var dogs =(IEnumerable<Dog>)dataGrid.Items 
    //don't know how you match them up from your question, but you should be able to find a dog 
    match = dogs.FirstOrDefault(d => d.BreedName == selectedBreed.Name); 
    If(match != null) 
     //do your update 
+0

我將它轉換爲字符串,只讓我可以看到價值,就是這樣。 –

+0

當你把它轉換成它自己的類型時,你可以看到所有東西:) =>你可以根據它的類型,值等來操作它。 –

相關問題