This project has moved. For the latest updates, please go here.

[AvalonDock 2.0] Binding the title of a LayoutDocument from a collection

Dec 9, 2014 at 7:44 AM
I'm binding an ObservableCollection to AvalonDock 2.0, where every item in the collection is an AvalonDock Document. This is how I do the binding:
<ad:DockingManager DocumentsSource="{Binding Path=OpenProjects, Mode=TwoWay}" ActiveContent="{Binding Path=CurrentProject, Mode=TwoWay}" LayoutItemTemplateSelector="{StaticResource ProjectTemplateSelector}">
...
</ad:DockingManager>
The problem is I want to show the Name of each item (which is specified in the property Name in CurrentProject) as the Document title. This is what I've tried:
<ad:DockingManager.DocumentHeaderTemplate>
    <DataTemplate>
        <TextBlock DataContext="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ad:DockingManager}, Path=ActiveContent, Mode=OneWay}" Text="{Binding Path=Name}" />
    </DataTemplate>
</ad:DockingManager.DocumentHeaderTemplate>
This works fine if I only have one document open, but when I have several, they all show the Name of the current project. For example, if I have four open projects, with the names "A", "B", "C" and "D", if I'm currently viewing the document "C", all four tabs will show the title "C", and when I change to document "B", they will all change its names to "B".

Is there any way to prevent this changes? I've tried setting the binding mode to OneTime, but it doesn't seem to work.
Developer
Dec 9, 2014 at 4:19 PM
Hi,

Can you just use the Content.Title, not the Name of the ActiveContent :
 public partial class MainWindow : Window
{
    public MainWindow()
    {
      InitializeComponent();

      _avalon.DataContext = this;

      this.OpenProjects = new ObservableCollection<LayoutDocument>()
      {
        new LayoutDocument() { Title = "First", Content = "Test First"},
        new LayoutDocument() { Title = "Second", Content = "Test Second"},
        new LayoutDocument() { Title = "Third", Content = "Test Third"},
        new LayoutDocument() { Title = "Fourth", Content = "Test Fourth"},
      };
    }

    public ObservableCollection<LayoutDocument> OpenProjects
    {
      get;
      set;
    }
}

  <ad:DockingManager x:Name="_avalon"
                            DocumentsSource="{Binding Path=OpenProjects, Mode=TwoWay}">
            <ad:DockingManager.DocumentHeaderTemplate>
               <DataTemplate>
                  <TextBlock Text="{Binding Content.Title}" />
               </DataTemplate>
            </ad:DockingManager.DocumentHeaderTemplate>
</ad:DockingManager>
Dec 11, 2014 at 2:54 PM
Thanks for the response, but is there any way to do this without codebehind? (using pure MVVM would be the ideal).

Thank you!