简体   繁体   中英

How to get SelectionChanged event from ComboBox in DataGrid in right way?

I have a model

public class UCClipProcessingModel : BaseModel
{
    public ObservableCollection<ClipProcessingGridItem> GridItems { get; }
            = new ObservableCollection<ClipProcessingGridItem>();
}

and there is an items

public class ClipProcessingGridItem: IValidable
{
    public MCClipFolder ClipFolder { get; set; }

    public MCGeoCalibFolder SelectedGeoCalibrationFolder { get; set; } = MCGeoCalibFolder.EMPTY();

    public ObservableCollection<MCGeoCalibFolder> GeoCalibrationFolders { get; set; }
            = new ObservableCollection<MCGeoCalibFolder>();

    public MCColorCalibFolder SelectedColorCalibrationFolder { get; set; } = MCColorCalibFolder.EMPTY();

    public ObservableCollection<MCColorCalibFolder> ColorCalibrationFolders { get; set; }
            = new ObservableCollection<MCColorCalibFolder>();

    public bool IsValid()
    {
        return true;
    }
}

So, in my .xalm as a Context I am using UCClipProcessingModel and for my DataGrid I use GridItems each element of this ObservableCollection it is acctually an one row in my DataGrid .

Now, in my row I have a such DataGridTemplateColumn

...
<DataGridTemplateColumn Header="Geometry calibration folder">
  <DataGridTemplateColumn.CellTemplate>
    <DataTemplate>
      <ComboBox x:Name="Cb_geometry_calibration"
                SelectionChanged="Cb_geometry_calibration_SelectionChanged"
                ItemsSource="{Binding Path=GeoCalibrationFolders}"
                SelectedItem="{Binding Path=SelectedGeoCalibrationFolder}">
        <ComboBox.ItemTemplate>
          <DataTemplate>
            <TextBlock Text="{Binding Path=UIRepresentation}" />
          </DataTemplate>
        </ComboBox.ItemTemplate>
      </ComboBox>
    </DataTemplate>
  </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
...

There is a screenshot

在此处输入图像描述

Now I need to know value when user changed it in ComboBox , what I can do in order to get it? I set SelectionChanged method

private void Cb_geometry_calibration_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    if (((sender as ComboBox).SelectedItem) is MCGeoCalibFolder itemm)
    {
        Console.WriteLine($"Item clicked: {itemm.ToString()}");
    }
}

And all is fine I can get value that was changed, but problem is that I don't know with which ClipProcessingGridItem from ObservableCollection this value associated...

Question is - How to know with which element a changed value associated?

You could cast the DataContext to whatever type your data item is:

var comboBox = sender as ComboBox;
var item = comboBox.DataContext as ClipProcessingGridItem;

Or simply get rid of the event handler and handle your logic in the setter of SelectedGeoCalibrationFolder . This is how you would solve this using MVVM.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM