RadContextMenu in data grid cell

3 posts, 0 answers
  1. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 28 Jul 2009 Link to this post

    When the context menu is used inside a datagrid cell, it stops working. Take a look at the following code, I've included some information in the xaml about the behaviour of the menu. Please make sure this is fixed as soon as possible, as it prevents us from demonstrating our app to clients.

    Best regards,

    <UserControl x:Class="TelerikTestProject.SilverlightControl5" 
            <DataTemplate x:Key="Template"
                <TextBlock Text="Menu" 
                        <teleriknav:RadContextMenu EventName="MouseLeftButtonDown" 
                                <teleriknav:RadMenuItem Header="Item 1"></teleriknav:RadMenuItem> 
                                <teleriknav:RadMenuItem Header="Item 2"></teleriknav:RadMenuItem> 
        <Grid x:Name="LayoutRoot" 
                <TextBlock HorizontalAlignment="Left" 
                           Text="The behaviour of the context menu is erratic. Very often a click in the menu does not trigger an event as it should.  
    Some observations:&#x0a; 
    * The first of the three menus does not work when the page is loaded.&#x0a; 
    * The first menu starts working if another cell is clicked and the menu is opened immediately.&#x0a; 
    * None of the menus work for more than one click. As soon as a click is registered, the 
    event chain dies. Only when another cell is focused do the menus start working again."></TextBlock
                <TextBlock x:Name="Result" 
                <data:DataGrid x:Name="DataGrid1" 
                        <data:DataGridTemplateColumn CellTemplate="{StaticResource Template}"></data:DataGridTemplateColumn> 
                        <data:DataGridTextColumn Binding="{Binding}"></data:DataGridTextColumn> 

    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; 
    namespace TelerikTestProject 
        public partial class SilverlightControl5 : UserControl 
            private int _ClickCount = 0; 
            public SilverlightControl5() 
                this.RemoveHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(RadMenu_ItemClick)); 
                this.AddHandler(RadMenuItem.ClickEvent, new RoutedEventHandler(RadMenu_ItemClick), true); 
                DataGrid1.ItemsSource = new List<string>() { "First row""Second row""Third row" }; 
                Result.Text = String.Format("{0} clicks made.", _ClickCount.ToString()); 
            public void RadMenu_ItemClick(object sender, RoutedEventArgs e) 
                Result.Text = String.Format("{0} clicks made.", _ClickCount.ToString()); 

  2. Boyan
    Boyan avatar
    758 posts

    Posted 03 Aug 2009 Link to this post

    Hello hwsoderlund,

    First I would like to apologize for the late response. We are very busy work on the next service pack which will be our official release for Silverlight 3.
    I've tested your scenario and I was able to find the problem. The issue is that DataGrid use different templates for showing and editing the content. Thus when you click on the cell the context menu is opened and the template of the cell is changed to the edit template (even if you don't have editingTempalte set). Then clicking on the menu item raise the Click routed event but the visual tree for this context menu reach to the TextBlock (on which ContextMenu is attached) and stop because the old (non editing template is shown) templated is removed from the visual tree.
    Clicking on the second row open the menu and it works as expected only the first time. Next click triggers the cell to go to edit mode and again the template is replaced.
    Right now I don't have solution to this issue but if you tell me your case I can think of something.
    Also it is not good to attach the ContextMenu in DataTemplate because new context menu is created every time the template is used.
    My suggestion will be to attach the menu to the DataGrid or to attach new ContextMenu to every DataGridRow.

    All the best,
    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. DevCraft banner
  4. hwsoderlund
    hwsoderlund avatar
    419 posts
    Member since:
    Aug 2006

    Posted 03 Aug 2009 Link to this post

    For anyone experiencing similar problems, here's a solution: Set IsReadOnly to true in the DataGridTemplateColumn. This will avoid the edit template messing up the routed event.

Back to Top