I wrote an addin that exports a document and a toolbar. Now I want the toolbar to be visible only if current active document is of my type. How can I get the active document from layout manager/workbench?

asked 03 May '12, 13:35

Dmitry%20Arestov's gravatar image

Dmitry Arestov
accept rate: 0%

Getting the current document might not be necessary if all you want to do is make a toolbar visible/hidden. The toolbars have a VisibleCondition property, which I haven't played with, but might be exactly what you're looking for.

That being said, I briefly wondered recently in my SBC adventures, how I would reference the current document, although I have not yet needed to actually do it. (My post from last week needed an instance of a View to reference the matching instance of its ViewModel, which is a similar issue but not exactly the same).

I have found (empirically, mind you) that switching tabs triggers the DataContextChanged event for content within the data template, where the new context is the new document. You could add an OnTabChanged method in your document class and use a static variable to track the current document. Going back to the ol' Recipe example (abbreviated)...


    <DataTemplate DataType="{x:Type local:Recipe}">
        <local:RecipeViewControl DataContextChanged="Content_DataContextChanged" />


    public void Content_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        var newDocument = e.NewValue as IDocument;
        var oldDocument = e.OldValue as IDocument;

        // post the TabChanged to the document manager
        Recipe.OnTabChanged(oldDocument, newDocument);

        // the tab that we switched to is the one that triggers this event
        // sending the ViewModel to the View in this way might not actually be necessary, 
        // but it's working for me
        if (newDocument == null) return;
        var view = sender as RecipeViewControl;


    private static IDocument _currentDocument;
    public IDocument CurrentDocument
        get { return _currentDocument; }

    public static void OnTabChanged(IDocument oldDocument, IDocument newDocument)
        _currentDocument = newDocument;

It would be nice to have some of this more generic, which you could do by a) write a DocumentManager service, b) extend the AbstractDocument class with the new methods, and c) wire them together. Seems like this is common enough that it might be desirable to have in SoapBox Core.


answered 03 May '12, 15:16

drewkeller's gravatar image

accept rate: 0%

Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported



Asked: 03 May '12, 13:35

Seen: 2,105 times

Last updated: 03 May '12, 15:16

powered by OSQA