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

ContextMenu, can't use MenuItem by code..

1 Answer 17 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Kennet
Top achievements
Rank 2
Kennet asked on 10 Jan 2014, 10:29 AM
Hi,

I have a Togglebutton with an Image in a cell that has a Contexmenu that opens when I Left click on tit. That works fine. But I need to set in code "IsEnable" on the MenuItems, but I can't use the MenuItems name, the code cant see / find the element (The name 'menu1' does not exist in the current context).

I used a Togglebutton since I found that to be the easiest way, what I need is an image that will open an ContexMenu on left click.

This How I code in Xaml

<telerik:GridViewColumn Header="Mode"  Width="60" HeaderTextAlignment="Center">
            <telerik:GridViewColumn.CellTemplate>
                <DataTemplate>
 
                    <ToggleButton Name="btnMode" HorizontalAlignment="Center" IsChecked="{Binding IsOpen, ElementName=menuModes, Mode=TwoWay}" Tag="{Binding Id}" Click="btnMode_Click">
                        <Image Source="{Binding ModeImage}"/>
                        <telerik:RadContextMenu.ContextMenu>
                            <telerik:RadContextMenu Name="menuModes" Placement="Bottom">
                                <telerik:RadMenuItem Header="Menu 1" Tag="{Binding Id}"  Name="menu1" Click="menu1_Click"  />
                                <telerik:RadMenuItem Header="Menu 2" Tag="{Binding Id}" Name="menu2" Click="menu2_Click" />
                                <telerik:RadMenuItem Header="Menu 3" Tag="{Binding Id}" Name="menu3" Click="menu3_Click" />
                            </telerik:RadContextMenu>
                        </telerik:RadContextMenu.ContextMenu>
                    </ToggleButton>
 
                </DataTemplate>
            </telerik:GridViewColumn.CellTemplate>
        </telerik:GridViewColumn>

/Ken

1 Answer, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 15 Jan 2014, 11:30 AM
Hi Ken,

You can subscribe to RadContextMenu's Loaded event and find a RadMenuItem using the ChildrenOfType<> extension method. Please check the following code snippet for a reference:
private void menuModes_Loaded(object sender, RoutedEventArgs e)
     {
         var menuItem = (sender as RadContextMenu).ChildrenOfType<RadMenuItem>().Where(m => m.Name == "menu2").FirstOrDefault();
         menuItem.IsEnabled = false;
     }

Moreover, you can check this blog post explaining how to work with the ChildrenOfType<T>() and ParentOfType<T>() extension methods. 

I hope this helps.

Regards,
Yoan
Telerik
TRY TELERIK'S NEWEST PRODUCT - EQATEC APPLICATION ANALYTICS for WPF.
Learn what features your users use (or don't use) in your application. Know your audience. Target it better. Develop wisely.
Sign up for Free application insights >>
Tags
GridView
Asked by
Kennet
Top achievements
Rank 2
Answers by
Yoan
Telerik team
Share this question
or