Find parent RadPane of visual element

3 posts, 0 answers
  1. Ben
    Ben avatar
    3 posts
    Member since:
    Aug 2013

    Posted 01 Oct 2014 Link to this post

    I added a new button to my pane header (a 'help' button) using the following link: http://www.telerik.com/help/silverlight/raddocking-how-to-add-buttons-to-the-pane-headers.html

    I also created a custom routed ui command using the following link: http://www.telerik.com/help/silverlight/raddocking-how-to-add-menu-items-to-the-radpanes-menu.html

    However, I had some trouble finding the RadPane containing the clicked RadButton in the pane header. I'm sure there is a much better or simpler way to find the current pane. Here is my solution, but could someone tell me what the proper method is?

    Template attached to RadPane:

     <DataTemplate x:Key="TitleTemplate">
            <Grid>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <ContentPresenter Content="{Binding}"
                              Margin="0,0,10,0" />
                <telerik:RadButton Style="{StaticResource helpButton}" Grid.Column="1" HorizontalAlignment="Right"
                                   Command="common:HelpCommand.OpenHelpTooltipCommand" CommandParameter="{Binding}" CommandTarget="{Binding}">
                    <Image Style="{StaticResource imghelpButton}" />
                </telerik:RadButton>
            </Grid>
        </DataTemplate>

    Registered command:

    CommandManager.RegisterClassCommandBinding(typeof(RadButton),
                    new CommandBinding(HelpCommand.OpenHelpTooltipCommand, HelpCommand.OnOpenHelpTooltip, HelpCommand.OnOpenHelpTooltipCanExecute));

    Custom command:

        public static class HelpCommand
        {
            private static RoutedUICommand openHelpTooltipCommand;

            public static RoutedUICommand OpenHelpTooltipCommand
            {
                get
                {
                    if (openHelpTooltipCommand == null)
                    {
                        openHelpTooltipCommand = new RoutedUICommand("Open help tooltip", "OpenHelpTooltipCommand", typeof(HelpCommand));
                    }
                    return openHelpTooltipCommand;
                }
            }

            public static void OnOpenHelpTooltip(object sender, ExecutedRoutedEventArgs e)
            {
                var btn = e.OriginalSource as RadButton;
                if (btn != null)
                {
                    var parent = btn.GetVisualParent<Telerik.Windows.Controls.Docking.ToolWindow>();
                    if (parent != null)
                    {
                        var docking = parent.GetParentDocking();
                        if (docking != null)
                        {
                            foreach (var pane in docking.Panes)
                            {
                                if ((string)pane.Title == (string)btn.DataContext)
                                {
                                    var control = pane.Content; //THERE MUST BE A BETTER WAY TO GET HERE!!!
                                }
                            }
                        }
                    }
                }
            }

            public static void OnOpenHelpTooltipCanExecute(object sender, CanExecuteRoutedEventArgs e)
            {
                e.CanExecute = true;
            }
        }
  2. Vladi
    Admin
    Vladi avatar
    744 posts

    Posted 02 Oct 2014 Link to this post

    Hi Eddie,

    Thank you for contacting us.

    The easiest approach in getting the RadPane instance by an element in it's TitleTemplate is to used the PaneHeader element of that template and use it's GetParentPane() method. The next code snippet shows how the OnOpenHelpTooltip method from your code snippet should look like:

    using Telerik.Windows.Controls.Docking;
    ...
     
    public static void OnOpenHelpTooltip(object sender, ExecutedRoutedEventArgs e)
    {
        var btn = e.OriginalSource as RadButton;
        if (btn != null)
        {
            var paneHeader = btn.ParentOfType<PaneHeader>();
            if (paneHeader != null)
            {
                var pane = paneHeader.GetParentPane();
            }
        }
    }

    Hope this is helpful.

    Regards,
    Vladi
    Telerik
     
    Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
     
  3. DevCraft banner
  4. Ben
    Ben avatar
    3 posts
    Member since:
    Aug 2013

    Posted 02 Oct 2014 in reply to Vladi Link to this post

    Thanks Vladi!

    Your solution actually did not work correctly for me, but it helped lead me to another one that did:

    http://www.telerik.com/forums/radpane-titletemplate-with-button-and-custom-event#wKMKSmkTBEeiuwM6VF8y8A

    Apparently if the RadPane is floating or docked, it takes a different method to access it from the TitleTemplate
Back to Top