Reputation: 5243
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?
Upvotes: 1
Views: 226
Reputation: 169220
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.
Upvotes: 2