Additonal button on AvalonDock control

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

需要在这里添加按钮


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;
}

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

  • 链接地址: http://www.djcxy.com/p/57398.html

    上一篇: 我如何判断一个Type是否实现IList <>?

    下一篇: AvalonDock控件上的添加按钮