Question

I've troubles with a data binding in my WPF application. The situation is:

I've two controls, one with a tree view, and other with a detailed view of the item selected on the list. Everything was working (binding) with the following code:

<avalonDock:LayoutAnchorablePaneGroup DockWidth="230" Orientation="Vertical">     
    <avalonDock:LayoutAnchorablePane Name="CTTreeLayout">
        <avalonDock:LayoutAnchorable Title="CTs">
            <treeControl:TreeViewUControl x:Name="TreeViewCTs">
            </treeControl:TreeViewUControl>
        </avalonDock:LayoutAnchorable>
    </avalonDock:LayoutAnchorablePane>
    <avalonDock:LayoutAnchorablePane Name="CTInfoLayout">
        <avalonDock:LayoutAnchorable Title="Información">
            <infoControl:EntityInfoUControl x:Name="InfoControl" SelectedItem="{Binding ElementName=TreeViewCTs, Path=CTViewModel.SelectedItem}">
            </infoControl:EntityInfoUControl>
        </avalonDock:LayoutAnchorable>
    </avalonDock:LayoutAnchorablePane>
</avalonDock:LayoutAnchorablePaneGroup>

Selected item was a dependency property on the details control.

Now, I'm changing the layout strategy with Avalondock (as you could see from avalonDock tags in code). That implies using template selectors. Layout works correctly but binding is broken. Here is the actual piece of code, with each control within their template:

<avalonDock:DockingManager.LayoutItemTemplateSelector>
    <layout:PanesTemplateSelector>
        <layout:PanesTemplateSelector.CTToolTemplate>
            <DataTemplate>
                <treeControl:TreeViewUControl x:Name="TreeViewCTs"/>
            </DataTemplate>
        </layout:PanesTemplateSelector.CTToolTemplate>
        <layout:PanesTemplateSelector.CTInfoToolTemplate>
            <DataTemplate>
                <infoControl:EntityInfoUControl x:Name="InfoControl" SelectedItem="{Binding CTViewModel.SelectedItem, ElementName=TreeViewCTs}"/>
            </DataTemplate>
        </layout:PanesTemplateSelector.CTInfoToolTemplate>
    </layout:PanesTemplateSelector>
</avalonDock:DockingManager.LayoutItemTemplateSelector>

How could I update the binding to work again? I've tried with FindAncestor without success.

EDIT:

Solved finally. As Dean Chalk said, my goal was not possible following my strategy. After a few headaches I refactorized my data model. Now I've a view model that includes the tree view and the details view. I'm following the structure of the AvalonDock 2.0 MVVM sample.

Sample here

Was it helpful?

Solution

Using a DataTemplate like this you will only be able to bind to whatever gets set as the DataContext at run-time. You wont have visibility on the SelectedItem of another control, and you wont be able to use ElementName bindings.

There really isn't a straightforward solution to this kind of problem, and you will need to look long and hard at your Data model.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top