Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
45
How to create Dragtemplate for both multiple and single selected drag node.
posted

I am currently creating two data templates one for single selected item from drag source and another for multiple selected items drag source and assigning thses templates to dragtemplate of drag source on drag start event.

but this is not working for me please let me know if there is any suggestion.

my drag start event code.

private void AssociatedObject_DragStart(object sender, DragDropStartEventArgs e)
{
DragSource dragsource = sender as DragSource ;
var dragNodes = new List<TreeNode>();

//
// TreeGrid has both "Selected Items" and an "Active Item".
// "Selected Items" only seems to be non-null when more than one item is selected.
// "Active Item" is non-null when at least one item is highlighted.
//
if (_dataPresenter.SelectedItems?.Any() ?? false)
{
foreach (var selectedRecord in _dataPresenter.SelectedItems)
{
if (selectedRecord is DataRecord dr &&
dr.DataItem is TreeNode node)
{
dragNodes.Add(node);
}
else if (selectedRecord is Cell c &&
c.Record is DataRecord drc &&
drc.DataItem is TreeNode nodec)
{
dragNodes.Add(nodec);
}
}
}
else if (_dataPresenter.ActiveRecord != null)
{
if (_dataPresenter.ActiveRecord is DataRecord dr &&
dr.DataItem is TreeNode node)
{
dragNodes.Add(node);
}
}

//
// Drag is only allowed if all nodes allow it.
//
dragsource.DragTemplate = DragSingleElementTemplate;
if (dragNodes.Any() &&
dragNodes.All(n => n.Attributes.HasFlag(TreeNodeAttributes.Drag)))
{
e.Data = dragNodes.ToArray();
if (dragNodes.Count>1)
{
dragsource.DragTemplate = DragMultipleElementTemplate;
}
}
else
{
e.Cancel = true;
}
dragsource.DragTemplate.LoadContent();
}

  • 33830
    Offline posted

    Hello Avinash,

    I have been investigating the sample code that you have provided, and I am a bit unsure of why the switch of the DragTemplate would not work for you in this case, as setting it within DragStart should be valid.

    I have a sample project that does multiple selection with different drag templates. I will attach it here, as perhaps following the code provided will help you in this case.

    Please let me know if you have any other questions or concerns on this matter.

    MultiSelectDragDropXamDataGridBehavior.zip