ContextMenu for 2 TreeView - bug with BeginEdit

4 posts, 1 answers
  1. Adrien
    Adrien avatar
    2 posts
    Member since:
    May 2009

    Posted 15 May 2009 Link to this post

    I am trying to implement your example of a context menu on a tree view. When I have two tree views, Right Click and Edit on an item make it in edit mode and back immediately, whereas editing an item with F2 works as it should.

    If one of the tree views doesn't contain any items, entering in edit mode with a right click on the other tree view works well.

    In your sample solution Examples.ContextMenu.CS, if I add a second tree view I can edit via the context menu on both of thems without problems.

    I am using RadControls_for_Silverlight_2009_1_0413, with Silverlight 2. The problems occurs with IE6/7, Firefox 3.0.10 and Windows 2000 and XP.

    Can you tell me what is missing in my solution ? Thanks you.

    Here is how to reproduce the bug :

    Page.xaml
    <UserControl x:Class="SilverlightApplication2.Page" 
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"  
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
        xmlns:telerikNavigation="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
        Width="600" Height="400"
        <Grid x:Name="LayoutRoot" Background="White"
            <StackPanel Orientation="Horizontal"
                <telerikNavigation:RadTreeView x:Name="xTreeViewA" IsEditable="True"
                    <telerikNavigation:RadContextMenu.ContextMenu> 
                        <telerikNavigation:RadContextMenu> 
                            <telerikNavigation:RadMenuItem Header="Add New Item"
                                <telerikNavigation:RadMenuItem Header="Add Sibling" /> 
                                <telerikNavigation:RadMenuItem Header="Add Child" /> 
                            </telerikNavigation:RadMenuItem> 
                            <telerikNavigation:RadMenuItem Header="Delete Item" /> 
                            <telerikNavigation:RadMenuItem Header="Edit" /> 
                        </telerikNavigation:RadContextMenu> 
                    </telerikNavigation:RadContextMenu.ContextMenu> 
                    <telerikNavigation:RadTreeViewItem Header="Root"
                        <telerikNavigation:RadTreeViewItem Header="Folder"
                            <telerikNavigation:RadTreeViewItem Header="File" /> 
                            <telerikNavigation:RadTreeViewItem Header="File" /> 
                        </telerikNavigation:RadTreeViewItem> 
                    </telerikNavigation:RadTreeViewItem> 
                </telerikNavigation:RadTreeView> 
                <telerikNavigation:RadTreeView x:Name="xTreeViewB" IsEditable="True"
                    <telerikNavigation:RadContextMenu.ContextMenu> 
                        <telerikNavigation:RadContextMenu> 
                            <telerikNavigation:RadMenuItem Header="Add New Item"
                                <telerikNavigation:RadMenuItem Header="Add Sibling" /> 
                                <telerikNavigation:RadMenuItem Header="Add Child" /> 
                            </telerikNavigation:RadMenuItem> 
                            <telerikNavigation:RadMenuItem Header="Delete Item" /> 
                            <telerikNavigation:RadMenuItem Header="Edit" /> 
                        </telerikNavigation:RadContextMenu> 
                    </telerikNavigation:RadContextMenu.ContextMenu> 
                    <telerikNavigation:RadTreeViewItem Header="Root"
                        <telerikNavigation:RadTreeViewItem Header="Folder"
                            <telerikNavigation:RadTreeViewItem Header="File" /> 
                            <telerikNavigation:RadTreeViewItem Header="File" /> 
                        </telerikNavigation:RadTreeViewItem> 
                    </telerikNavigation:RadTreeViewItem> 
                </telerikNavigation:RadTreeView> 
            </StackPanel> 
        </Grid> 
    </UserControl> 
     

    Page.xaml.cs
    using System; 
    using System.Collections.Generic; 
    using System.Linq; 
    using System.Net; 
    using System.Windows; 
    using System.Windows.Controls; 
    using System.Windows.Documents; 
    using System.Windows.Input; 
    using System.Windows.Media; 
    using System.Windows.Media.Animation; 
    using System.Windows.Shapes; 
    using Telerik.Windows; 
    using Telerik.Windows.Controls; 
    using Telerik.Windows.Input; 
     
    namespace SilverlightApplication2 
        public partial class Page : UserControl 
        { 
            public Page() 
            { 
                InitializeComponent(); 
                xTreeViewA.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClicked)); 
                xTreeViewB.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(OnMenuItemClicked)); 
                Mouse.AddMouseDownHandler(this, OnRightMouseButtonUp); 
            } 
     
            private void OnRightMouseButtonUp(object sender, Telerik.Windows.Input.MouseButtonEventArgs e) 
            { 
                RadTreeViewItem treeItem = null
                Point mousePosition = e.GetPosition(null); 
                foreach (UIElement item in VisualTreeHelper.FindElementsInHostCoordinates(mousePosition, this)) 
                { 
                    treeItem = item as RadTreeViewItem; 
                    if (treeItem != null
                    { 
                        break
                    } 
                } 
     
                if (treeItem != null
                { 
                    treeItem.IsSelected = true
                } 
            } 
     
            private void OnMenuItemClicked(object sender, RoutedEventArgs args) 
            { 
                RadTreeView vParentTreeView = sender as RadTreeView; 
                RadRoutedEventArgs e = args as RadRoutedEventArgs; 
                RadMenuItem item = e.OriginalSource as RadMenuItem; 
                if ((item != null)) 
                { 
                    if (item.Header != null
                    { 
                        if (item.Header.ToString() == "Add Sibling"
                        { 
     
                            RadTreeViewItem treeViewItem = vParentTreeView.SelectedItem as RadTreeViewItem; 
                            if (treeViewItem != null
                            { 
                                if (treeViewItem.Parent is RadTreeViewItem) 
                                { 
                                    (treeViewItem.Parent as RadTreeViewItem).Items.Add(new RadTreeViewItem() { Header = "New Item" }); 
                                } 
                                else 
                                { 
                                    (treeViewItem.Parent as RadTreeView).Items.Add(new RadTreeViewItem() { Header = "New Item" }); 
                                } 
     
                            } 
                        } 
     
                        if (item.Header.ToString() == "Add Child"
                        { 
     
                            RadTreeViewItem treeViewItem = vParentTreeView.SelectedItem as RadTreeViewItem; 
                            if (treeViewItem != null
                            { 
                                treeViewItem.Items.Add(new RadTreeViewItem() { Header = "New Item" }); 
                                treeViewItem.IsExpanded = true
                            } 
                        } 
                        if (item.Header.ToString() == "Delete Item"
                        { 
                            RadTreeViewItem treeViewItem = vParentTreeView.SelectedItem as RadTreeViewItem; 
                            if (treeViewItem != null
                            { 
                                if (treeViewItem.ParentItem != null
                                { 
                                    treeViewItem.ParentItem.Items.Remove(treeViewItem); 
                                } 
                                else 
                                { 
                                    treeViewItem.ParentTreeView.Items.Remove(treeViewItem); 
                                } 
                            } 
                        } 
                        if (item.Header.ToString() == "Edit"
                        { 
                            RadTreeViewItem treeViewItem = vParentTreeView.SelectedItem as RadTreeViewItem; 
                            if (treeViewItem != null
                            { 
                                treeViewItem.BeginEdit(); 
                            } 
                        } 
                    } 
                } 
            } 
        } 
     

  2. Answer
    Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 16 May 2009 Link to this post

    Hi Adrien,

    Yes - we are aware of this behavior. It is expected behacior since the editing is very coupled with the focused element in the application. thus when set manually on user interaction there should be take special care for it.

    Just replace the treeViewItem.BeginEdit(); with this code:

    Dispatcher.BeginInvoke(() =>
        {
             treeViewItem.BeginEdit();
        }
    );

    Regards,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
  3. Bruno
    Bruno avatar
    7 posts
    Member since:
    May 2009

    Posted 01 Jul 2009 Link to this post

    Hi Valentin,

    I'm trying to implement the exact same behaviour as Adrien (BeginEdit via ContextMenu) with the difference that my tree is data bounded to a business object collection. Even if I call BeginEdit asynchronously via the Dispatcher, my tree node does not change to EditMode :(

    I've noticed on the following post
                 http://blogs.telerik.com/blogs/09-04-27/attaching_a_contextmenu_on_a_treeview_with_radcontrols_for_silverlight.aspx
    that Valeri Hristov says that the "RadTreeView contains a bug that breaks the editing when the control is data bound...".

    Do you confirm this? Is there any workaround so that I can edit the selected node of my data bounded tree without having to press F2?

    Cheers,
    Bruno

  4. Valentin.Stoychev
    Admin
    Valentin.Stoychev avatar
    2198 posts

    Posted 03 Jul 2009 Link to this post

    Hello Bruno,

    Thank you for your interest in our controls. Yesterday we released our official Q2 release, where we have a new property called ItemEditTemplate. Now you can define your own template for the editing. Also you can use the IsInEditmode property to run the tree node into edit mode. Please download the latest binaries and let us know if you still have problems.

    Regards,
    Valentin.Stoychev
    the Telerik team

    Instantly find answers to your questions on the new Telerik Support Portal.
    Check out the tips for optimizing your support resource searches.
Back to Top