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

Can't find context menu in Visual Tree

4 Answers 313 Views
Menu
This is a migrated thread and some comments may be shown as answers.
Raj
Top achievements
Rank 1
Raj asked on 28 Aug 2009, 04:42 PM
I'm trying to programmatically access a button that's part of the data template of a RadMenuItem. I thought the easiest thing would be to use the VisualTreeHelper to search the ContextMenu for the button. However, I can't find the context menu in this manner. Even using tools such as Silverlight Spy I'm unable to find the menu in the visual tree. Any ideas?

Thanks,
Raj

4 Answers, 1 is accepted

Sort by
0
Valeri Hristov
Telerik team
answered on 31 Aug 2009, 03:46 PM
Hi Raj,

RadContextMenu is not in the visual tree until you open it, that's why it is not visible in Silverlight Spy. Why do you need to find it in the visual tree?

All the best,
Valeri Hristov
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
Raj
Top achievements
Rank 1
answered on 31 Aug 2009, 04:03 PM
Hi Valeri,

Here's my scenario - I've created a data template for my menu items with a textblock and a button as follows... 

<telerik:RadMenuItem x:Name="mnuTest"
    <telerik:RadMenuItem.HeaderTemplate> 
        <DataTemplate> 
            <StackPanel Orientation="Horizontal" x:Name="spTest"
                <TextBlock Text="Testing" VerticalAlignment="Center" /> 
                <Button Content="&gt;" x:Name="btnTest" Width="20" /> 
            </StackPanel> 
        </DataTemplate> 
    </telerik:RadMenuItem.HeaderTemplate> 
</telerik:RadMenuItem> 

Essentailly I would like to have each menu contain two events: one when you click on the textblock and one when you click on the button (to show sub-mneu items). It does not seem that I can programmatically attach these event handlers since I cannot get a reference to the button.
0
Accepted
Valeri Hristov
Telerik team
answered on 01 Sep 2009, 10:11 AM
Hi Raj,

You could achieve the desired functionality with an attached behavior: declare an attached property in some class in your application that when set on a button will attach Click event handler and execute the code you need. The xaml would be something like this:

<Button ... local:MyClickBehavior.IsEnabled="true" />

in the property changed callback of the IsEnabled property you will have a reference to the button and will be able to manipulate it like you wish.

Regards,
Valeri Hristov
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
Raj
Top achievements
Rank 1
answered on 09 Sep 2009, 07:15 PM
Hi Valeri - this works great - thank you. I've attached some code for other folks who may want to do something similar...

public class MenuBehavior: DependencyObject  
    {  
        public static readonly DependencyProperty IsEnabledProperty; 
 
        static MenuBehavior()  
        {  
            IsEnabledProperty = DependencyProperty.RegisterAttached(  
                "IsEnabled",   
                typeof(bool), 
                typeof(MenuBehavior), 
                new PropertyMetadata(false, OnBehaviorEnabled));  
        }  
      
        public static void SetIsEnabled( DependencyObject obj, bool isEnabled ) 
        { 
            obj.SetValue(IsEnabledProperty, isEnabled); 
        } 
 
        public static bool GetIsEnabled( DependencyObject obj ) 
        { 
            return (bool)obj.GetValue(IsEnabledProperty); 
        } 
 
 
        private static void OnBehaviorEnabled(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args) 
        { 
            Button btn = (Button)dependencyObject; 
            btn.Click += new RoutedEventHandler(btn_Click); 
        } 
 
        static void btn_Click(object sender, RoutedEventArgs e) 
        { 
            MessageBox.Show("Button Clicked"); 
        }  
    }  

<telerik:RadMenuItem.HeaderTemplate> 
                                            <DataTemplate> 
                                                <StackPanel Orientation="Horizontal" x:Name="spTest"
                                                    <TextBlock Text="Testing" VerticalAlignment="Center" HorizontalAlignment="Left" /> 
                                                    <Button Content="&gt;" x:Name="btnTest" Width="20" 
                                                        VerticalAlignment="Center" HorizontalAlignment="Right" Margin="0,0,0,10" 
                                                        behavior:MenuBehavior.IsEnabled="true"
                                                         
                                                    </Button> 
                                                </StackPanel> 
                                                </DataTemplate> 
                                        </telerik:RadMenuItem.HeaderTemplate> 

Tags
Menu
Asked by
Raj
Top achievements
Rank 1
Answers by
Valeri Hristov
Telerik team
Raj
Top achievements
Rank 1
Share this question
or