MVVM problem when clearing items

Docking/MDI for WPF Forum

Posted 13 years ago by SledgeHammer01
Version: 11.1.0543
Platform: .NET 4.0
Environment: Windows 7 (32-bit)
Avatar
I have the following public properties in my VM:

public ObservableCollection<DocumentViewModelBase> Documents
{
get
{
if ((object)_lstDocuments == null)
_lstDocuments = new ObservableCollection<DocumentViewModelBase>();

return _lstDocuments;
}
}

public DocumentViewModelBase SelectedDocument
{
get
{
return _selectedVM;
}

set
{
if (_selectedVM != value)
{
_selectedVM = value;
OnPropertyChanged("SelectedDocument");
}
}
}

The documents property is bound like this:

<docking:DockSite local:DockSiteViewModelBehavior.IsManaged="True" themes:ThemeManager.Theme="{Binding ElementName=mainWindow, Path=(themes:ThemeManager.Theme)}"
DocumentItemsSource="{Binding Documents}" DocumentItemContainerStyle="{StaticResource DocumentItemStyle}">

The SelectedDocument property is bound like this:

<docking:Workspace Focusable="False" FocusVisualStyle="{x:Null}">
<docking:TabbedMdiHost Focusable="False">
<docking:TabbedMdiContainer SelectedValue="{Binding SelectedDocument}" />
</docking:TabbedMdiHost>
</docking:Workspace>

This all works properly. Document tabs are added properly, SelectedDocument is updated in the VM and updating it in the VM selects the tab as expected. SelectedDocument getter is called and returns the correct VM.

PROBLEM: I have a "logoff" command. The logoff command calls Documents.Clear() as one of its steps to logoff. When I call Documents.Clear(), it closes all the document windows as expected, but it seems like you lose the SelectedDocument binding. The getter is no longer called and SelectedDocuments no longer sets the active tab.

When I call Documents.Clear(), I see the following binding error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='ActiproSoftware.Windows.Controls.Docking.TabbedMdiContainer', AncestorLevel='1''. BindingExpression:Path=SelectedItem; DataItem=null; target element is 'DockingWindowTabPanel' (Name=''); target property is 'SelectedTab' (type 'UIElement')

If I call everything in my Logoff function EXCEPT Documents.Clear(), of course the tabs on the screen lose the content, but SelectedItem binding is not lost.

Also tried removing the VMs from Documents one by one and same problem with SelectedDocument binding getting lost.

Comments (1)

Posted 13 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Hello,

You cannot bind to the TabbedMdiContainer's SelectedValue property. The TabbedMdiContainer, ToolWindowContainer, and SplitContainer elements are dynamically created and destroyed as windows are moved around, added, and removed. In addition, you could have any number of TabbedMdiContainers with selected documents. Even with tool windows, there isn't a concept of a single "selected" window.

You would need to either manually synchronize your SelectedDocument property with the TabbedMdiHost.PrimaryWindow or the DockSite.ActiveWindow properties. Each of these properties has an associated "changed" event, which you can use to update your view-model. This can be done in the DockSiteViewModelBehavior to encapsulate the functionality.

On a side note, you should not need to set the ThemeManager.Theme property on the DockSite. Setting it on the Window should have the same affect. For setting the theme globally, you should use ThemeManager.CurrentTheme instead.


Actipro Software Support

The latest build of this product (v24.1.1) was released 1 month ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.