Additonal button on AvalonDock control

887 views Asked by At

Does adding button on avalondock windows title bar can be done? coudn't find any resources on it either. I have done adding button on title bar of normal WPF windows, but avalondock has no option for adding button. I have an old application which needed to have help button on title bar like this

need to add buttons here

1

There are 1 answers

4
Mitya On

Let's assume you have DockablePane

<ad:DockingManager>
    <ad:DockablePane x:Name="myAwesomePane">
        <ad:DockableContent >
            ... SomeContent ...
        </ad:DockableContent>
    </ad:DockablePane>
</ad:DockingManager>

Then after your window has been loaded you can find DockPanel in your pane visual tree and add button to it.

private void MainWindow_OnLoaded(object sender, RoutedEventArgs e)
{
    var dockPanel = FindChild<DockPanel>(myAwesomePane);
    if (dockPanel != null)
    {
        var button = new Button {Content = "Help", Margin = new Thickness(1), Width = 40};
        button.Click += (o, args) => MessageBox.Show(this, "HELP");
        DockPanel.SetDock(button, Dock.Right);
        dockPanel.Children.Insert(dockPanel.Children.Count - 1, button);
    }
}

/// <summary>
/// Find first child of given type
/// </summary>
public static T FindChild<T>(DependencyObject parent) where T : DependencyObject
{
    var childrenCount = VisualTreeHelper.GetChildrenCount(parent);

    T result = null;
    for (int i = 0; i < childrenCount; i++)
    {
        var dObj = VisualTreeHelper.GetChild(parent, i);

        result = dObj as T;
        if (result != null)
        {
            break;
        }

        result = FindChild<T>(dObj);
        if (result != null)
        {
            break;
        }
    }

    return result;
}

Help button But there are several limitations I didn't look up for solution:

  • button disappears after pane was docked to another place

  • you have to do it for all your panes manually