[英]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
.因此,在我的
.xalm
作为上下文中,我使用UCClipProcessingModel
,对于我的DataGrid
,我使用GridItems
这个ObservableCollection
的每个元素,它实际上是我的DataGrid
中的一行。
Now, in my row I have a such DataGridTemplateColumn
现在,在我的行中,我有一个这样的
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?现在我需要知道用户在
ComboBox
中更改它时的值,我可以做些什么才能得到它? I set SelectionChanged
method我设置
SelectionChanged
方法
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...一切都很好,我可以获得已更改的值,但问题是我不知道该值与
ObservableCollection
中的哪个ClipProcessingGridItem
关联...
Question is - How to know with which element a changed value associated?问题是 -如何知道更改值与哪个元素相关联?
You could cast the DataContext
to whatever type your data item is:您可以将
DataContext
转换为数据项的任何类型:
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
.或者简单地摆脱事件处理程序并在
SelectedGeoCalibrationFolder
的设置器中处理您的逻辑。 This is how you would solve this using MVVM.这就是使用 MVVM 解决此问题的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.