Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Binding the title of a LayoutDocument from a collection in AvalonDock 2.0

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.

like image 940
Johnny Clara Avatar asked Dec 02 '22 18:12

Johnny Clara


2 Answers

What I ended up doing was as simple as this:

<ad:DockingManager.DocumentHeaderTemplate>
    <DataTemplate>
        <TextBlock Text="{Binding Content.Name}" />
    </DataTemplate>
</ad:DockingManager.DocumentHeaderTemplate>

Explanation: the DataContext for the binding inside the DocumentHeaderTemplate is the LayoutDocument itself. Turns out that it has a property, named Content, which represents de binding object inside each document (in this case, each Project inside my OpenProjects collection). In that object, I've got the property Name which is the string I want to use for the title.

like image 114
Johnny Clara Avatar answered Jan 10 '23 21:01

Johnny Clara


This is because you are binding the title text to a property of the object referenced via the ActiveContent of your docking manager. It's obvious that changing the ActiveContent (focusing documents) will update the title of all LayoutDocument views to same value, because all titles are bound to same source.

You could try something like this:

<ad:DockingManager.DocumentHeaderTemplate>
    <DataTemplate>
        <Border x:Name="Header">
            <ad:AnchorablePaneTitle Model="{Binding Model, RelativeSource={RelativeSource TemplatedParent}}"/>
        </Border>
    </DataTemplate>
</ad:DockingManager.DocumentHeaderTemplate>
like image 25
dymanoid Avatar answered Jan 10 '23 21:01

dymanoid