Skip to content

Commit 91957b5

Browse files
authored
Merge pull request #762 from telerik/new-kb-navigationview-drag-drop-customize-312a116756d346b88b2a193f90ede1c7
Added new kb article navigationview-drag-drop-customize
2 parents 5d6108b + c157bd3 commit 91957b5

File tree

1 file changed

+115
-0
lines changed

1 file changed

+115
-0
lines changed
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Preventing Nested Drag and Drop in NavigationView Pages
3+
description: Learn how to prevent nested drag and drop and pin a footer page in the RadNavigationView for UI for WinForms.
4+
type: how-to
5+
page_title: How to Avoid Nested Drag and Pin Footer Page in RadNavigationView
6+
meta_title: How to Avoid Nested Drag and Pin Footer Page in RadNavigationView
7+
slug: navigationview-drag-drop-customize
8+
tags: navigationview, ui-for-winforms, drag-drop, footer, custom-drag-drop-service
9+
res_type: kb
10+
ticketid: 1691649
11+
---
12+
13+
|Product Version|Product|Author|
14+
|----|----|----|
15+
|2025.2.520|NavigationView for WinForms|[Dinko Krastev](https://www.telerik.com/blogs/author/dinko-krastev)|
16+
17+
## Description
18+
19+
By default, the RadNavigationView control allows you to reorder the navigation view items according to your preferences. The drag-drop behavior of the RadNavigationView control is achieved by the RadPageViewDragDropService, which can be accessed by the RadNavigationView1.NavigationViewElement.ItemDragService property. If customization is required, we will need to create a custom class that derives from RadPageViewDragDropService and override the methods of the drag-drop service.
20+
21+
In the following scenario, the drag-drop should be performed on the first level only, thus disabling the option to have nested drag-drop. Also, the footer item should not be draggable.
22+
23+
## Solution
24+
25+
To achieve this, create a custom drag-and-drop service and override specific methods. Follow these steps:
26+
27+
1. **Prevent Nested Dragging and Dropping:**
28+
Implement a custom class that derives from `RadPageViewDragDropService`. Override the `CanDragOver` method to restrict drop positions and prevent nested pages.
29+
30+
````C#
31+
32+
public class CustomDragDropService : RadPageViewDragDropService
33+
{
34+
public CustomDragDropService(RadPageViewElement owner) : base(owner)
35+
{
36+
}
37+
38+
protected override bool CanDragOver(RadPageViewItem pageViewItem)
39+
{
40+
if (!(pageViewItem is RadPageViewNavigationViewItem radPageViewNavigationViewItem))
41+
{
42+
return true;
43+
}
44+
45+
if (!radPageViewNavigationViewItem.Enabled || !radPageViewNavigationViewItem.AllowDrop)
46+
{
47+
return false;
48+
}
49+
50+
if (radPageViewNavigationViewItem.PinPosition == NavigationViewItemPinPosition.Footer)
51+
{
52+
return false;
53+
}
54+
55+
DropPosition dropPosition = GetDropPosition(radPageViewNavigationViewItem, base.DropLocation);
56+
if (dropPosition == DropPosition.AsChildNode)
57+
{
58+
return false;
59+
}
60+
61+
return base.CanDragOver(pageViewItem);
62+
}
63+
64+
protected internal virtual DropPosition GetDropPosition(RadPageViewNavigationViewItem radPageViewNavigationViewItem, Point dropLocation)
65+
{
66+
int num = radPageViewNavigationViewItem.Size.Height / 3;
67+
int y = dropLocation.Y;
68+
if (y < num)
69+
{
70+
return DropPosition.BeforeNode;
71+
}
72+
73+
if (y >= num && y <= num * 2)
74+
{
75+
return DropPosition.AsChildNode;
76+
}
77+
return DropPosition.AfterNode;
78+
}
79+
}
80+
81+
````
82+
83+
2. **Disable Dragging for Footer Page:**
84+
Override the `OnPreviewDragStart` method to check the `PinPosition` property. Cancel the drag operation for the footer page.
85+
86+
````C#
87+
88+
protected override void OnPreviewDragStart(PreviewDragStartEventArgs e)
89+
{
90+
var radPageViewNavigationViewItem = e.DragInstance as RadPageViewNavigationViewItem;
91+
if (radPageViewNavigationViewItem != null)
92+
{
93+
if (radPageViewNavigationViewItem.PinPosition == NavigationViewItemPinPosition.Footer)
94+
{
95+
e.CanStart = false;
96+
}
97+
}
98+
base.OnPreviewDragStart(e);
99+
}
100+
101+
````
102+
103+
3. **Assign Custom Drag-and-Drop Service:**
104+
Set the custom service to the RadNavigationView control.
105+
106+
````C#
107+
108+
radNavigationView1.NavigationViewElement.ItemDragService = new CustomDragDropService(radNavigationView1.NavigationViewElement);
109+
110+
````
111+
112+
## See Also
113+
114+
* [RadNavigationView Overview Documentation](https://docs.telerik.com/devtools/winforms/controls/navigationview/overview)
115+
* [RadPageViewDragDropService API Reference](https://docs.telerik.com/devtools/winforms/api/html/T_Telerik_WinControls_UI_RadPageViewDragDropService.htm)

0 commit comments

Comments
 (0)