To get DataGridCell data, you can use its DataContext and Column property. How to do this, it exactly depends on the data of your string, i.e. What items do you put in the ItemsSource DataGrid collection. Assuming your elements are object[] arrays:
If your data in a row is more complex, you need to write an appropriate method that converts the Column property and row data element to a specific value in the row data element.
EDIT:
If the cell you are dropping data into is not the selected cell, one of them is to get the DataGridRow to which the DataGridCell belongs, using VisualTreeHelper :
var parent = VisualTreeHelper.GetParent(gridCell); while(parent != null && parent.GetType() != typeof(DataGridRow)) { parent = VisualTreeHelper.GetParent(parent); } var dataRow = parent;
Then you have a line and can act as above.
Also, regarding your question about whether to revise the method, I would suggest using WPF custom behavior.
Behavior provides a very direct way to expand the control capabilities from C # code, not XAML, but at the same time your code is easy and simple (which is not only nice if you follow MVVM). Behaviors are designed to be reusable and not tied to your specific control.
Here is a good introduction
In your specific case, I can only give you an idea of ββwhat to do:
Write one DropBehavior for your TextBlock control (or any other control that you want to use in your DataGridCells that handles the drop. The main idea is to register actions performed by evnt cells in the OnAttached() method of your control.
public class DropBehavior : Behavior<TextBlock> { protected override void OnAttached() { AssociatedObject.MouseUp += AssociatedObject_MouseUp; } private void AssociatedObject_MouseUp(object sender, MouseButtonEventArgs e) {
Note that parsing single cell data from row data and the Column property is becoming obsolete.
Then you attach this behavior to the TextBlocks that you put in your cells using the ContentTemplate CellStyle your DataGrid:
<DataGrid> <DataGrid.CellStyle> <Style TargetType="DataGridCell"> <Setter Property="ContentTemplate"> <Setter.Value> <DataTemplate> <TextBlock Text="{Binding}"> <i:Interaction.Behaviors> <yourns:DropBehavior/> </i:Interaction.Behaviors> </TextBlock> </DataTemplate> </Setter.Value> </Setter> </Style> </DataGrid.CellStyle> </DataGrid>
Bactex Behavior<T> can be found in
System.Windows.Interactivity.dll
I have not tested it, but I hope this works for you and you get the idea ...