This is a migrated thread and some comments may be shown as answers.

Get the RadTreeViewItem being Edited

2 Answers 121 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Ziad
Top achievements
Rank 1
Ziad asked on 07 Jan 2010, 08:45 AM
In the method signature for the event handler for the event 'Edited' there are the following parameters: ( object sender, RadTreeViewItemEditedEventArgs e ).

How can I get the RadTreeViewItem that is being edited? e.OriginalSource returns an 'EditableHeaderedItemsControl' and sender returns the RadTreeView. Is there something I am missing?

Thank you.

EDIT:

OK, I was able to cast e.OriginalSource to RadTreeViewItem. The problem that I am encountering is that I want to delete the node if its header is empty. So inside the Edited event handler I have the following code:

item.ParentItem.Items.Remove(item); 

I get the following null exception:

   at Telerik.Windows.Controls.RadTreeViewItem.CommitEdit() in c:\Builds\WPF_Scrum\Navigation_WPF\Sources\Development\Controls\Navigation\TreeView\RadTreeViewItem.cs:line 718
   at Telerik.Windows.Controls.RadTreeView.HandleItemSelectionFromUI(RadTreeViewItem treeViewItemToSelect) in c:\Builds\WPF_Scrum\Navigation_WPF\Sources\Development\Controls\Navigation\TreeView\RadTreeView.cs:line 2196
   at Telerik.Windows.Controls.RadTreeViewItem.OnHeaderMouseLeftButtonDown(Object sender, MouseButtonEventArgs e) in c:\Builds\WPF_Scrum\Navigation_WPF\Sources\Development\Controls\Navigation\TreeView\RadTreeViewItem.Events.cs:line 790
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
   at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e)
   at System.Windows.UIElement.OnMouseDownThunk(Object sender, MouseButtonEventArgs e)
   at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
   at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
   at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
   at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
   at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
   at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
   at System.Windows.Input.InputManager.ProcessStagingArea()
   at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
   at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
   at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
   at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
   at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
   at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
   at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
   at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
   at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
   at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at System.Windows.Window.ShowHelper(Object booleanBox)
   at System.Windows.Window.Show()
   at System.Windows.Window.ShowDialog()


2 Answers, 1 is accepted

Sort by
0
Accepted
Kiril Stanoev
Telerik team
answered on 08 Jan 2010, 03:30 PM
Hello Ziad,

In this case you need to use a Dispatcher as shown bellow:

private void RadTreeView_Edited(object sender, Telerik.Windows.Controls.RadTreeViewItemEditedEventArgs e)
{
    RadTreeViewItem item = e.OriginalSource as RadTreeViewItem;
             
    Dispatcher.BeginInvoke(() =>
    {
        item.ParentItem.Items.Remove(item);
    });
}

Let me know how this works for you.

Regards,
Kiril Stanoev
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Ziad
Top achievements
Rank 1
answered on 11 Jan 2010, 08:17 AM
Hello Kiril,

Thank you for your reply. I tried using the dispatcher to delete the tree item and it worked as expected. I just wanted to point out that the code you provided was not compiling.

So what I did was modify it as follows:

Dispatcher.BeginInvoke(DispatcherPriority.Send, new ThreadStart(()=> 
    item.ParentItem.Items.Remove(item); 
})); 

Is the above piece of code the correct way of implementing the delete? I chose the 'send' dispatcher priority because I want the item to be deleted as soon as possible.

Thank you.
Tags
TreeView
Asked by
Ziad
Top achievements
Rank 1
Answers by
Kiril Stanoev
Telerik team
Ziad
Top achievements
Rank 1
Share this question
or