title: MVVM Execute Command on ViewModel from other ViewModel
body: <p>I'm struggling for about 14 days now with a simple task: In database, I have definitions for hardware categories. For example :</p>
  
  <ol>
  <li>HDD
  <ul>
  <li>Internal</li>
  <li>External</li>
  <li>Flash</li>
  </ul></li>
  </ol>
  
  <p>This list is in database defined like this:</p>
  
  <pre><code>    [ID - ParrentID - Name] : 1 - 0 - HDD, 2 - 1 - Internal, 3 - 1 - External, 4 - 1 - Flash.        
  </code></pre>
  
  <p>Through Entity Framework I get these rows into my application. From this flat data I then create structured object which is my DataModel. This model is defined as follows :  </p>
  
  <pre><code>public class Category
  {
     private int _id = -1;
     private string _name = "";
     private List&lt;Category&gt; _subCategories = null;
  // property getters and setters, constructors, and bool HasSubCategories
  }  
  </code></pre>
  
  <p>Now, from these I create ViewModel called <em>SubCategoryViewModel</em> to which is binded my TreeView. So, I can view my categories in treeview and with my defined and maintained hierarchy. This works just fine. In <em>SubCategoryViewModel</em> is defined a <strong>Command</strong> through <em>Attached Behavior for MouseDoubleClick</em> which is also binded to TreeView. So, when user doubleclicks on Item, in <em>SubViewCategoryModel</em> defined method will execute particular code. List of  <em>SubCategoryViewModel</em> is nested in <em>HWDocumentViewModel</em> which is a main ViewModel for my window. 
  What I need now is obvious : When user doubleclicks on item in TreeView, I need to load items from database and show them in ListView. My opinion is, that in <em>HWDocumentViewModel</em> I need to define an collection of Items and load them accordingly to selected category in ListView. But, I don't know how to execute a method on HWDocumentViewModel from <em>SubCategoryViewModel</em>. Because : TreeView is binded to list of <em>SubCategoryViewModel</em> items, so when DoubleClick occurs, the method on <em>SubCategoryViewModel</em> is executed. I'm searching for a way, how to execute a method on main ViewModel (HWDocumentViewModel). </p>
  
  <p>I tried this approach :<br>
  I created a property : <code>public static SubCategoryViewModel SelectedCategory</code> on  <code>HWDocumentViewModel</code>. When doubleclick occurs, I set this property from SubCategoryViewModel as <code>this</code>. So, in this property is object, which executed doubleclick event delegate. Great, now I have in HWDocumentView model an object, which user selected.<br>
  So, I need to load items to ListView. But, will I load them from method in <em>SubCategoryViewModel</em> ? I don't think so. Instead I should load them from Main View Model  by creating a ViewModel for them and bind it to ListView, right ? But, how can I from SubCategoryViewModel call a method in HWDocumentViewModel ? Should I write a static method
  on a HWDocumentViewModel which will be accessible from SubCategoryViewModel ?<br>
  <strong>Or is there a way, how to call Command defined on HWDocumentViewModel from SubCategoryViewModel ?</strong></p>
  
  <p>Or generally, did I take a right approach to create a Warehouse-like application in WPF ?</p>
  
  <p>Thanks a lot.</p>
  
  <p>EDIT: XAML for my TreeView looks like this :</p>
  
  <pre><code>&lt;TreeView x:Name="tvCategories" Background="White" ItemsSource="{Binding Categories}"&gt;
                      &lt;TreeView.ItemContainerStyle&gt;
                          &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
                              &lt;Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" /&gt;
                              &lt;Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /&gt;
                              &lt;Setter Property="FontWeight" Value="Normal" /&gt;
                              &lt;Setter Property="behaviors:MouseDoubleClick.Command"  Value="{Binding MouseDoubleClickCommand}" /&gt;
                              &lt;Setter Property="behaviors:MouseDoubleClick.CommandParameter" Value="{Binding}" /&gt;
                              &lt;Style.Triggers&gt;
                                  &lt;Trigger Property="IsSelected" Value="True"&gt;
                                      &lt;Setter Property="FontWeight" Value="Bold" /&gt;
                                  &lt;/Trigger&gt;
                              &lt;/Style.Triggers&gt;
                          &lt;/Style&gt;
                      &lt;/TreeView.ItemContainerStyle&gt;
                      &lt;TreeView.Resources&gt;
                          &lt;HierarchicalDataTemplate DataType="{x:Type localvm:SubCategoryViewModel}" ItemsSource="{Binding Children}"&gt;
                              &lt;StackPanel Orientation="Horizontal"&gt;
                                  &lt;TextBlock Text="{Binding CategoryName}" /&gt;
                              &lt;/StackPanel&gt;
                          &lt;/HierarchicalDataTemplate&gt;
                      &lt;/TreeView.Resources&gt;
  
                  &lt;/TreeView&gt;
  </code></pre>