Home Forums WPF controls Xceed DataGrid for WPF Drag and Drop Cell Hit Testing

Viewing 4 posts - 1 through 4 (of 4 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #23025 |

    I am using Xceed grid in TableView mode and need for my users to be able to drag data from elsewhere and drop it on the grid.

    I have hooked into the Grid’s Drop event (and DragOver and DragEnter). These events are successfully fired and handled. However, I am having trouble identifying which Row and Column the drop event occurred.

    by looking at the Grid’s InputHitTest (which returns the TextBlock element that it was dropped on) and also the VisualTreeHelper.HitTest (which returns the TextBlock, Border, TableViewScrollViewer and another Border if I iterate through the visual elements) I have not been able to find this data.

    Anyone have any experience on how to obtain the Column and Row that the received the drop?

    Imported from legacy forums. Posted by Derek (had 3666 views)

    User (Old forums)
    Member
    Post count: 23064

    By crawling up the visual tree, you should eventually hit the Row. I’ll add a – tested and working! – example in the following post. Note that the custom GetParent is rather extensive and overkilled for this purpose. There’s no need to take the TemplatedParent path in a Drag/Drop context. You may trim the code as you see fit.

    Imported from legacy forums. Posted by Michel (had 532 views)

    User (Old forums)
    Member
    Post count: 23064

    private void GridDrop( object sender, DragEventArgs e )
    {
    string value = e.Data.GetData( typeof( string ) ) as string;

    if( value != null )
    {
    IInputElement element = this.grid.InputHitTest( e.GetPosition( this.grid ) );
    DependencyObject parent = element as DependencyObject;

    while( parent != null )
    {
    if( parent is Cell )
    {
    Cell cell = ( Cell )parent;
    Row row = cell.ParentRow;
    Column column = cell.ParentColumn;

    // Do your stuff !

    break;
    }

    parent = this.GetParent( parent );
    }
    }
    }

    public DependencyObject GetParent( DependencyObject element )
    {
    Visual visual = element as Visual;
    DependencyObject parent = ( visual == null ) ? null : VisualTreeHelper.GetParent( visual );

    if( parent == null )
    {
    // No Visual parent. Check in the logical tree.
    FrameworkElement fe = element as FrameworkElement;

    if( fe != null )
    {
    parent = fe.Parent;

    if( parent == null )
    {
    parent = fe.TemplatedParent;
    }
    }
    else
    {
    FrameworkContentElement fce = element as FrameworkContentElement;

    if( fce != null )
    {
    parent = fce.Parent;

    if( parent == null )
    {
    parent = fce.TemplatedParent;
    }
    }
    }
    }

    return parent;
    }

    Imported from legacy forums. Posted by Michel (had 987 views)

    User (Old forums)
    Member
    Post count: 23064

    Thanks for that – works as expected.

    Imported from legacy forums. Posted by Derek (had 4221 views)

Viewing 4 posts - 1 through 4 (of 4 total)
  • You must be logged in to reply to this topic.