0

我有一个填充了来自一个表的值的 DataGrid 和一个包含从 ObservableCollection (不同的表)填充的 ComboBox 的 TemplateColumn。这肯定不是最优雅的方法,但因为我没有时间从零开始并开始使用 MVVM 方法......

比方说:餐桌狗和餐桌狗品种

网格包含来自 Dogs 表和 Dog_breeds 组合框的值

当在 ComboBox 中更改选择时,我需要更改 Dogs 表中的 id_dog_breed ,因此:

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

  }

我怎样才能做到这一点?我相信这里的某个地方一定有一个类似的问题,很久以前就已经回答过了,但我没有找到它。

4

1 回答 1

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
于 2013-06-02T15:27:45.497 回答