WPF/AvalonDock v2.0 LayoutDocument

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12493117/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 05:25:10  来源:igfitidea点击:

WPF/AvalonDock v2.0 LayoutDocument

wpfavalondock

提问by fex

How can I add programmatically LayoutDocument with some of UIElements inside it? (like stackpanel, scrollviewer etc.) I'd like to add new LayoutDocument with stackpanel, canvas etc. to LayoutDocumentPane when user clicks "New project" button. May I somehow clone xaml code from one LayoutDocument and load it's to new one? And is it possible to bind Title LayoutDocument property to ViewModel Property? ( i get error it has to be dependency property )

如何以编程方式添加其中包含一些 UIElements 的 LayoutDocument?(如堆栈面板、滚动查看器等)当用户单击“新建项目”按钮时,我想将带有堆栈面板、画布等的新 LayoutDocument 添加到 LayoutDocumentPane。我可以以某种方式从一个 LayoutDocument 克隆 xaml 代码并将其加载到新的吗?是否可以将 Title LayoutDocument 属性绑定到 ViewModel 属性?(我得到错误它必须是依赖属性)

回答by Jaime Marín

You can use Contentproperty. For example if you want to add a new LayoutDocumentwith a custom content (StackPanele.g.) you could do it as follow:

您可以使用Content财产。例如,如果您想添加LayoutDocument具有自定义内容的新 内容(StackPanel例如),您可以按如下方式进行:

//Get the main LayoutDocumentPane of your DockingManager 
var documentPane = dockManager.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();

if (documentPane != null)
{
    LayoutDocument layoutDocument = new LayoutDocument {Title = "New Document"};

    //*********Here you could add whatever you want***********
    layoutDocument.Content = new StackPanel();

    //Add the new LayoutDocument to the existing array
    documentPane.Children.Add(layoutDocument);
}

回答by macloving

First, in XAML - give the name to the Grid, for example, x:Name = "mainGrid"

首先,在 XAML 中 - 为网格命名,例如,x:Name = "mainGrid"

Then in class write this

然后在课堂上写这个

        //Create button - we put this in document
        Button mybutton = new Button();
        mybutton.Content = "hello";
        mybutton.Width = 100;
        mybutton.Height = 50;
        mybutton.Click += (sender, ev) => { MessageBox.Show("Hello"); };


        DockingManager dockmanager = new DockingManager();

        //Set theme
        dockmanager.Theme = new Xceed.Wpf.AvalonDock.Themes.ExpressionLightTheme();

        //Create LayoutRoot
        var layoutroot = new Xceed.Wpf.AvalonDock.Layout.LayoutRoot();

        //Create LayoutPanel
        var layoutpanel = new Xceed.Wpf.AvalonDock.Layout.LayoutPanel();

        //Create LayoutDocumentPane
        var layoutdocpane = new Xceed.Wpf.AvalonDock.Layout.LayoutDocumentPane();

        //Create LayoutDocument and set parameters of Document
        var LayoutDocument = new Xceed.Wpf.AvalonDock.Layout.LayoutDocument();
        LayoutDocument.Title = "Some text";
        //Put button in Document
        LayoutDocument.Content = mybutton;

        layoutdocpane.Children.Add(LayoutDocument);
        layoutpanel.Children.Add(layoutdocpane);
        layoutroot.RootPanel.Children.Add(layoutpanel);

        dockmanager.Layout = layoutroot;
        mainGrid.Children.Add(dockmanager);

Sorry for my poor English. Please rewrite this, if it would be helpful.

对不起,我的英语不好。如果有帮助,请重写此内容。

回答by 1000ml

I'm not that familiar with WPF and especially AvalonDock. I did it like this and it works so far :)

You can write a separate class for your documents that inherits from LayoutDocument. In that way you should be able to edit the standard layout of your "Project-Document" with the VisualStudio Designer (add your stackpanel, canvas etc.).

(I assume that you have a standard way of displaying your "Project-Document". Otherwise you could build the content yourself in code behind like you would do in WPF and put it inside the LayoutDocument.)

我不太熟悉 WPF,尤其是 AvalonDock。我是这样做的,到目前为止它可以工作:)

您可以为继承自 LayoutDocument 的文档编写一个单独的类。通过这种方式,您应该能够使用 VisualStudio 设计器编辑“项目文档”的标准布局(添加您的堆栈面板、画布等)。

(我假设您有显示“项目文档”的标准方式。否则,您可以像在 WPF 中那样自己在代码中构建内容并将其放入 LayoutDocument 中。)

For example:

例如:

<ad:LayoutDocument x:Class="Namespace.MyDocument"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:ad="http://avalondock.codeplex.com"
    d:DesignHeight="500"
    d:DesignWidth="800"

<Grid>
    <!-- content -->
</Grid>

</ad:LayoutDocument>


And the class in code behind that inherits from LayoutDocument:


以及继承自 LayoutDocument 的代码中的类:

namespace Namespace
{
    public partial class MyDocument : AvalonDock.Layout.LayoutDocument
    {
        // ...
    }
}



To create and add a new document you just instantiate a new MyDocument object and add it to the collection via binding or something like layoutDocumentPane.Children.Add(doc).



要创建和添加新文档,您只需实例化一个新的 MyDocument 对象并通过绑定或类似layoutDocumentPane.Children.Add(doc).


I don't know about the binding for the Title Property, though.


不过,我不知道 Title 属性的绑定。

回答by Kumaravel

Thats exactly right. You can add the title by just adding doc.Title = "My document title"

完全正确。您只需添加即可添加标题doc.Title = "My document title"

or

或者

You can add Title="My document title"in the document.xamlwhich is going to be the child.

您可以添加Title="My document title"document.xaml这将是孩子。