[Version 2.0] How to get View for activecontent (ViewModel)

Jun 3, 2014 at 2:12 PM
Edited Jun 4, 2014 at 2:22 PM
First of all thanks for this great framework. Now I ran across one problem I could not find a solution yet.

I have set up an layout via databinding documents and toolwindows:
<xcad:DockingManager x:Name="dockingManager" Grid.Row="1" Theme="{Binding ElementName=_themeCombo, Path=SelectedItem.Value}"
                             AnchorablesSource="{Binding Path=Tools}"  
                             DocumentsSource="{Binding Path=DisplayedAsDocuments, Mode=TwoWay}" 
                             ActiveContent="{Binding Path=SelectedContent, Mode=TwoWay, Converter={StaticResource ActiveDocumentConverter}}" >
I want to use WPF Ribbon control to do some stuff, which works great as long as I execute commands on my mainviewmodel or child objects of my mainviewmodel. Now I have the situation that I open a new view for each document. A document could be a list of Images. The representation of the documents is selected via a Template and Style selector like in MVVM sample project. Now I want to bind certain ribbons commands to the current active view instance representation of my document which I can not, since i cant get the activeview nor is there a property for it on dockManager. I could not find a way to get the view by contentID either.

This is the template for the view that gets created for each document:
 <helper:PanesTemplateSelector.SinglePageDocumentViewTemplate>
     <DataTemplate>
          <fs:ViewerBox ItemsSource="{Binding Path=Document.Components[0].ComponentPages}"   />
     </DataTemplate>
 </helper:PanesTemplateSelector.SinglePageDocumentViewTemplate>
Here is a screenshot of my layout: you see three sample documents panes with one selected and above the ribbon which should get bound to the commands of the current active view
Image

EDIT: it seems the link to public onedrive image is not resolved... here the link without Imagetag http://1drv.ms/1hVDFdL

My current solution is to modify the datatemplate and adjust the datacontext of my ribbon in the windows codebehind
<helper:PanesTemplateSelector.SinglePageDocumentViewTemplate>
                        <DataTemplate>
                            <fs:ViewerBox ItemsSource="{Binding Path=Document.Components[0].ComponentPages}" IsVisibleChanged="ViewerBox_IsVisibleChanged" Loaded="ViewerBox_Loaded"  />
                        </DataTemplate>
                    </helper:PanesTemplateSelector.SinglePageDocumentViewTemplate>
private void ViewerBox_IsVisibleChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            FutureSoft.WindowsUI.ViewerBox viewer = sender as FutureSoft.WindowsUI.ViewerBox;
            bool bNewValue = Convert.ToBoolean(e.NewValue);

            if (bNewValue) {

                PreviousPageButton.DataContext = viewer;
                PagesMenuButton.DataContext = viewer;
                this.NextPageButton.DataContext = viewer;

                ZoomSlider.DataContext = viewer;
                ThumbnailsButton.DataContext = viewer;
                StandardSizeButton.DataContext = viewer;
                FitHeightButton.DataContext = viewer;
                FitWidthButton.DataContext = viewer;

                RotateLeftButton.DataContext = viewer;
                RotateRightButton.DataContext = viewer;
            }
}
This is all but comfortable nor does it work in all cases. If you dock mutliple document layoutitems next to each other so that all of them stay visible visibility aint changed and datacontext aint updated. Which means I have to find another fix. Would be WAY EASIER if there was a Property ActiveView on DockManager like in 1.3!

Adding GotFocus event in template and respective code in codebehind did the trick for now.
<helper:PanesTemplateSelector.SinglePageDocumentViewTemplate>
                        <DataTemplate>
                            <fs:ViewerBox ItemsSource="{Binding Path=Document.Components[0].ComponentPages}" IsVisibleChanged="ViewerBox_IsVisibleChanged" Loaded="ViewerBox_Loaded"  GotFocus="ViewerBox_GotFocus" />
                        </DataTemplate>
                    </helper:PanesTemplateSelector.SinglePageDocumentViewTemplate>

private void ViewerBox_GotFocus(object sender, RoutedEventArgs e)
        {
            // wenn er neu geladen wurde, wird er gleich sichtbar und aktiv
            FutureSoft.WindowsUI.ViewerBox viewer = sender as FutureSoft.WindowsUI.ViewerBox;

            if (PreviousPageButton.DataContext != viewer) {
                PreviousPageButton.DataContext = viewer;
                PagesMenuButton.DataContext = viewer;
                this.NextPageButton.DataContext = viewer;

                ZoomSlider.DataContext = viewer;
                ThumbnailsButton.DataContext = viewer;
                StandardSizeButton.DataContext = viewer;
                FitHeightButton.DataContext = viewer;
                FitWidthButton.DataContext = viewer;

                RotateLeftButton.DataContext = viewer;
                RotateRightButton.DataContext = viewer;
            }
        }