Dirkster99 / AvalonDock

Our own development branch of the well known WPF document docking library
Microsoft Public License
1.42k stars 321 forks source link

Set ActiveContent with ViewModel does not set focus #455

Open leo-schick opened 1 year ago

leo-schick commented 1 year ago

When I set the ActiveContext of DockingManager codewise with the view model of my documents (passed via binding in DocumentsSource), the focus is not switched to the page in the UI even I use Mode=TwoWay binding. But when I set ActiveContext of DockingManager with the LayoutDocumentPane class of the page, the focus is set. But that does not make sense in my opinion because the ActiveContext is set with the view model by the UI control (DockingManager) itself.

Here my XAML code:

            <avalonDock:DockingManager Grid.Column="2"
                                       DocumentsSource="{Binding DocumentPages}"
                                       AnchorablesSource="{Binding DetailPages}"
                                       ActiveContent="{Binding ActiveDocPage, Mode=TwoWay}">
                <avalonDock:DockingManager.LayoutItemTemplateSelector>
                    <kapok:PageTemplateSelector />
                </avalonDock:DockingManager.LayoutItemTemplateSelector>
                <avalonDock:DockingManager.LayoutItemContainerStyle>
                    <!-- you can add additional bindings from the LayoutItem to the DockWindowViewModel -->
                    <Style TargetType="{x:Type avalonDock:LayoutItem}">
                        <Setter Property="Title" Value="{Binding Model.Title}" />
                        <Setter Property="CanClose" Value="{Binding Model.CanClose, FallbackValue=True}" />
                        <Setter Property="CloseCommand" Value="{Binding Model.CloseAction, Converter={StaticResource IActionToICommandConverter}, Mode=OneTime}" />
                    </Style>
                </avalonDock:DockingManager.LayoutItemContainerStyle>

                <avalonDock:LayoutRoot>
                    <avalonDock:LayoutPanel>
                        <!-- default place for the main page(s) -->
                        <avalonDock:LayoutDocumentPane />

                        <!-- default place for detail pages -->
                        <avalonDock:LayoutAnchorablePaneGroup />
                    </avalonDock:LayoutPanel>
                </avalonDock:LayoutRoot>
            </avalonDock:DockingManager>

Here my code I use to set the focus on a document:

    public virtual void FocusDocumentPage(DocumentPageCollectionPage hostPage, IPage documentPage)
    {
        var window = GetOwnerWindow(hostPage);

        var dockingManager = window.FindVisualChildren<DockingManager>()?.FirstOrDefault() ?? throw new NotSupportedException("Could not get the avalon dock DockingManager");

        var firstDocumentPane = dockingManager.Layout.Descendents().OfType<LayoutDocumentPane>().FirstOrDefault();
        if (firstDocumentPane != null)
        {
            var documentPageLayoutDocument = firstDocumentPane.Children.FirstOrDefault(layoutDocument => layoutDocument.Content == documentPage);

            // We cannot set the view model but must set the LayoutDocument object
            dockingManager.ActiveContent = documentPageLayoutDocument;
        }
    }

p.s. DocumentPageCollectionPage and IPage are my view models.