Can't find context menu in Visual Tree

5 posts, 1 answers
  1. Raj
    Raj avatar
    34 posts
    Member since:
    Aug 2009

    Posted 28 Aug 2009 Link to this post

    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

  2. Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 31 Aug 2009 Link to this post

    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.
  3. DevCraft banner
  4. Raj
    Raj avatar
    34 posts
    Member since:
    Aug 2009

    Posted 31 Aug 2009 Link to this post

    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.
  5. Answer
    Valeri Hristov
    Admin
    Valeri Hristov avatar
    2252 posts

    Posted 01 Sep 2009 Link to this post

    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.
  6. Raj
    Raj avatar
    34 posts
    Member since:
    Aug 2009

    Posted 09 Sep 2009 Link to this post

    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> 

Back to Top
DevCraft banner