Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>For a Tab-based interface <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx" rel="nofollow">this classical article about MVVM pattern in WPF</a> might be very useful. (It also offers a downloadable sample application.)</p> <p>The basic idea to connect each tab with a UserControl is as follows (only a rough sketch, details are in the article):</p> <p>The MainWindow View has a ContentControl ...</p> <pre><code>&lt;ContentControl Content="{Binding Path=Workspaces}" ContentTemplate="{StaticResource WorkspacesTemplate}" /&gt; </code></pre> <p>... which binds to a collection of "Workspaces" in the MainWindowViewModel:</p> <pre><code>public ObservableCollection&lt;WorkspaceViewModel&gt; Workspaces { get; private set; } </code></pre> <p>This <code>WorkspaceViewModel</code> serves as a base class for all ViewModels you want to display as a tab.</p> <p>The <code>WorkspacesTemplate</code> is a DataTemplate which binds a TabControl to the collection of WorkspaceViewModels:</p> <pre><code>&lt;DataTemplate x:Key="WorkspacesTemplate"&gt; &lt;TabControl IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding}" /&gt; &lt;/TabControl&gt; &lt;/DataTemplate&gt; </code></pre> <p>And for every specific Tab you have a UserControl with a ViewModel which derives from WorkspaceViewModel ...</p> <pre><code>public class MySpecialViewModel : WorkspaceViewModel </code></pre> <p>... and which is related to the UserControl by a DataTemplate:</p> <pre><code>&lt;DataTemplate DataType="{x:Type vm:MySpecialViewModel}" &gt; &lt;v:MySpecialUserControl /&gt; &lt;/DataTemplate&gt; </code></pre> <p>Now, if you want to open a tab you would have a Command in the MainWindowViewModel which creates the ViewModel belonging to that tab and add it to the Workspaces collection of the MainWindowViewModel:</p> <pre><code>void CreateMySpecialViewModel() { MySpecialViewModel workspace = new MySpecialViewModel(); Workspaces.Add(workspace); } </code></pre> <p>The rest is done by the WPF binding engine. The TabControl recognizes automatically that this special workspace item in the collection is of type <code>MySpecialViewModel</code> and selects the right View/UserControl through the DataTemplate we have defined to connect ViewModel and View and displays it in a new Tab.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload