组合框选择中的WPF DataGrid传递值已更改

组合框选择中的WPF DataGrid传递值已更改,wpf,datagrid,combobox,Wpf,Datagrid,Combobox,我有一个DataGrid,它由一个表中的值填充,还有一个TemplateColumn,其中包含一个组合框,该组合框由ObservableCollection(不同的表)填充。这当然不是最优雅的方式,但因为我没有时间从零开始使用MVVM方法 比如说: 餐桌上的狗 & 桌上犬 网格保存表Dogs和Dog_繁殖的组合框中的值 当组合框中的选择更改时,我需要更改Dogs表中的id_dog_品种,以便: private void breed_combo_SelectionChanged(object se

我有一个DataGrid,它由一个表中的值填充,还有一个TemplateColumn,其中包含一个组合框,该组合框由ObservableCollection(不同的表)填充。这当然不是最优雅的方式,但因为我没有时间从零开始使用MVVM方法

比如说: 餐桌上的狗 & 桌上犬

网格保存表Dogs和Dog_繁殖的组合框中的值

当组合框中的选择更改时,我需要更改Dogs表中的id_dog_品种,以便:

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

  }

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

不要将它转换为字符串,将组合框中选定的项转换为它的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
private void breed\u combo\u SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
ComboBox ComboBox=(ComboBox)发送方;
var selectedbride=(狗\品种)组合框。SelectedValue;
//现在您可以访问该id并将其传递
//假设Id为property->selectedbride.Id
//你应该能够通过物品接近狗,我猜你的结构是什么
var dogs=(IEnumerable)dataGrid.Items
//我不知道你是如何从你的问题中找到它们的,但是你应该能够找到一只狗
match=dogs.FirstOrDefault(d=>d.BreedName==selectedbride.Name);
如果(匹配!=null)
//做你的更新

我将其转换为字符串只是为了能够看到值,仅此而已。当您将其转换为自己的类型时,您可以看到所有内容:)=>您可以根据其类型、值等对其进行操作。。