AvalonDock (2.0) + MVVM + VSPackage = problem with selecting a document?

I have a control that uses the AvalonDock (2.0) DockingManager to display a set of documents controlled by the underlying view model:

 <avalonDock:DockingManager DocumentsSource="{Binding Items}"> <avalonDock:DockingManager.LayoutItemContainerStyle> <Style TargetType="{x:Type avalonDockControls:LayoutItem}" BasedOn="{StaticResource DocumentItem}"/> </avalonDock:DockingManager.LayoutItemContainerStyle> <avalonDockLayout:LayoutRoot> <avalonDockLayout:LayoutPanel Orientation="Horizontal"> <avalonDockLayout:LayoutDocumentPane/> </avalonDockLayout:LayoutPanel> </avalonDockLayout:LayoutRoot> </avalonDock:DockingManager> 

The document view model has the IsSelected property, and when the view model is selected, I want to select and show the document in the DockingManager . To do this, I updated the Style for LayoutItem as follows:

 <Style x:Key="DocumentItem" TargetType="{x:Type avalonDockControls:LayoutItem}"> <Setter Property="Title" Value="{Binding Model.TabTitle}"/> <Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}"/> </Style> 

This approach works great when the control is hosted in WPF, but when the control is hosted in VSPackage, selecting a document in the view model most often does not cause the selected document to not be displayed. In the image below, the user clicked on the node client for editing, but the client document did not appear.

AvalonDock issue

The view model level sets IsSelected as expected, and when the control used TabControl , the selected document was always shown at the top.

This is a problem in AvalonDock. What I find is when LayoutItem selected when choosing a view model, the first document in the list is reset to true at the LayoutDocument/LayoutContent/LayoutElement , which seeps back and resets IsSelected to true in the LayoutItem layer. I would like to bind IsSelected to my view model at the LayoutDocument/LayoutContent/LayoutElement , but there is no DependencyProperty at this level.

I tried alternative methods of binding IsSelected to my view model, but so far without success. Anyone else run into this issue? Do you know about workarounds or other approaches? I would not want to resort to using an approach other than MVVM just to select a document.

Edit: It looks like this problem may be unclear, since it is difficult for me to create a test play example, even with VSPackage. In the following test case, which simulates the actual controls using two ToolWindow and Mediator messages, selecting a document works fine.

Tool windows

This test case (with a WPF application and VSPackage) is available here (click Tools=>My command name to launch the tool windows and enter the name of the document in one window to select or create and select a document in the window using the DockingManager ).

+4
source share
1 answer

I attach generosity to this, as I am sure that others will face this problem one way or another, and there should be a better solution.

At the moment, I have a terrible hack to get a selection of a document to work with:

  • I supported the same IsSelected binding with my LayoutItem style document LayoutItem .
  • In my main view model, I added the SelectedItem property to track which document was selected programmatically (through an editing event, etc.).
  • I added an event handler for ActiveContentChanged in the DocumentManager control, and in this method I re-select SelectedItem in my view model if it is not selected. AvalonDock tries to reinstall the first document in the list up to a dozen and a half times before giving up!
  • I added an event handler for PreviewMouseDown in the DocumentManager control, and in this method I will omit the SelectedItem view model so that the user-initiated function continues to work.

Again, looking for a better answer to this!

+2
source

All Articles