Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set ActiveContent with ViewModel does not set focus #455

Open
leo-schick opened this issue Sep 10, 2023 · 0 comments
Open

Set ActiveContent with ViewModel does not set focus #455

leo-schick opened this issue Sep 10, 2023 · 0 comments

Comments

@leo-schick
Copy link

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant