Я пытаюсь получить значение ячейки в таблице DataGrid, но после установки столбца как TemplateColumn выдается ошибка. Раньше у меня был столбец в виде ComboBoxColumn, и мой код работал.
Вот мой код C #:
DataGridRow Row = (DataGridRow)MYGRID.ItemContainerGenerator.ContainerFromIndex(MYGRID.SelectedIndex);
RowColumn = MYGRID.Columns[4].GetCellContent(Row).Parent as DataGridCell;
string Value = ((ComboBox)RowColumn.Content).Text;
Вот мой код XAML для столбца:
<DataGridTemplateColumn x:Name="ValueCol" Header="Value" >
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<ComboBox ItemsSource="{Binding Options, UpdateSourceTrigger=PropertyChanged}" SelectedItem="{Binding Path=UpdatedData, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, NotifyOnTargetUpdated=True}" IsEditable="False" LostFocus="ComboBox_LostFocus" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
При обновлении значения я получаю следующую ошибку:
'Unable to cast object of type 'System.Windows.Controls.ContentPresenter' to type 'System.Windows.Controls.ComboBox'.'
РЕДАКТИРОВАТЬ : удалось получить новое значение, создав событие LostFocus и извлекая данные из объекта RoutedEventArgs.