1

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 )

fex
  • 3,488
  • 5
  • 30
  • 46

4 Answers4

6

You can use Content property. For example if you want to add a new LayoutDocument with a custom content (StackPanel e.g.) you could do it as follow:

//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);
}
Jaime Marín
  • 578
  • 6
  • 11
5

First, in XAML - give the name to the Grid, for example, 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.

macloving
  • 1,227
  • 1
  • 18
  • 22
3

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.)

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:

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).


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

1000ml
  • 864
  • 6
  • 14
  • thanks but I've already achieved this with , class which inherit from DataTemplateSelector and DocumentSource={Binding Documents} in ad:Docking manager which is binding to my Document ViewModel. You can also easily bind to Title property using just by setting in Style.Setter Property="Title" to a value in your ViewModel. – fex Oct 05 '12 at 12:45
0

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

or

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

slavoo
  • 5,798
  • 64
  • 37
  • 39
Kumaravel
  • 36
  • 4