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

RadContextMenu in data grid cell

2 Answers 139 Views
Menu
This is a migrated thread and some comments may be shown as answers.
hwsoderlund
Top achievements
Rank 1
hwsoderlund asked on 28 Jul 2009, 04:18 PM
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,
/Henrik

<UserControl x:Class="TelerikTestProject.SilverlightControl5" 
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
             xmlns:telerikbase="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls" 
             xmlns:telerikinput="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Input" 
             xmlns:teleriknav="clr-namespace:Telerik.Windows.Controls;assembly=Telerik.Windows.Controls.Navigation" 
             xmlns:m="clr-namespace:System.Windows.Controls;assembly=System.Windows" 
             xmlns:data="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Data" 
             Width="500" 
             Height="400"
    <UserControl.Resources> 
        <DataTemplate x:Key="Template"
            <TextBlock Text="Menu" 
                       Margin="10,3,10,3" 
                       VerticalAlignment="Center"
                <teleriknav:RadContextMenu.ContextMenu> 
                    <teleriknav:RadContextMenu EventName="MouseLeftButtonDown" 
                                               Placement="Bottom"
                        <teleriknav:RadContextMenu.Items> 
                            <teleriknav:RadMenuItem Header="Item 1"></teleriknav:RadMenuItem> 
                            <teleriknav:RadMenuItem Header="Item 2"></teleriknav:RadMenuItem> 
                        </teleriknav:RadContextMenu.Items> 
                    </teleriknav:RadContextMenu> 
                </teleriknav:RadContextMenu.ContextMenu> 
            </TextBlock> 
        </DataTemplate> 
    </UserControl.Resources> 
    <Grid x:Name="LayoutRoot" 
          Background="White"
 
        <StackPanel> 
 
            <TextBlock HorizontalAlignment="Left" 
                       Margin="5" 
                       TextWrapping="Wrap" 
                       Width="350" 
                       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" 
                       Margin="5" 
                       FontWeight="Bold" 
                       Foreground="Green"></TextBlock> 
 
            <data:DataGrid x:Name="DataGrid1" 
                           AutoGenerateColumns="False" 
                           HorizontalAlignment="Stretch" 
                           VerticalAlignment="Stretch"
                <data:DataGrid.Columns> 
 
                    <data:DataGridTemplateColumn CellTemplate="{StaticResource Template}"></data:DataGridTemplateColumn> 
                    <data:DataGridTextColumn Binding="{Binding}"></data:DataGridTextColumn> 
 
                </data:DataGrid.Columns> 
            </data:DataGrid> 
 
        </StackPanel> 
    </Grid> 
</UserControl> 
 


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() 
        { 
            InitializeComponent(); 
 
            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) 
        { 
            _ClickCount++; 
            Result.Text = String.Format("{0} clicks made.", _ClickCount.ToString()); 
        } 
    } 
 

2 Answers, 1 is accepted

Sort by
0
Boyan
Telerik team
answered on 03 Aug 2009, 01:04 PM
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,
Boyan
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.
0
hwsoderlund
Top achievements
Rank 1
answered on 03 Aug 2009, 01:52 PM
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.

/Henrik
Tags
Menu
Asked by
hwsoderlund
Top achievements
Rank 1
Answers by
Boyan
Telerik team
hwsoderlund
Top achievements
Rank 1
Share this question
or