Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to Interact with 2 or More ViewModels
    primarykey
    data
    text
    <p>I have written a tool in which a ListBox is bound to a <code>ObserservableCollection&lt;object&gt;</code> with varying datatypes I've define. I use a <code>PropertyDataTemplateSelector</code> to present the data in the ListBox. The <code>PropertyDataTemplateSelector</code> references several <code>DataTemplates</code> that are set as <code>UserControls</code>. There is a background class that provides logic to the <code>PropertyDataTemplateSelector</code> by checking the <code>object</code> type and then applying the correct <code>DataTemplate</code>.</p> <p>Here's an abbreviated example of the XAML for the <code>UserControls</code> and the <code>MainWindow</code>.</p> <p>UserControl1 </p> <pre><code> &lt;TextBlock Text="{Binding Path=Val1}" Style="{StaticResourse Yes}" /&gt; &lt;Button Content="I'm Button 1" Command="{Binding Path=PathtoCommand1}" CommandParameter="{Binding Parameter1}" IsEnabled="{Binding IsEnabled1}" /&gt; &lt;Button Content="I'm Button 2" Command="{Binding Path=PathtoCommand2}" CommandParameter="{Binding Parameter2}" IsEnabled="{Binding IsEnabled2}" Tag="{Binding Path="DataContext.TagItem2}"&gt; &lt;Button.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem IsCheckable="True" IsChecked="{Binding Path=Tag}" DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" /&gt; &lt;/ContextMenu&gt; &lt;/Button.ContextMenu&gt; &lt;/Button&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>UserControlN</p> <pre><code>&lt;UserControl x:Class="AwesomerControl"&gt; &lt;StackPanel Orientation="Vertical"&gt; &lt;TextBlock Text="{Binding Path=FancyName2}" Style="{StaticResourse Yes}" /&gt; &lt;Button Content="Clicker 1" Command="{Binding Path=DoSomethingGreat1}" CommandParameter="{Binding Greatness1}" IsEnabled="{Binding IsTurnedOn1}" /&gt; &lt;Button Content="Clicker 2" Command="{Binding Path=DoSomethingGreat2}" CommandParameter="{Binding Greaterness2}" IsEnabled="{Binding IsTurnedOn2}" Tag="{Binding Path="DataContext.TagItem2}"&gt; &lt;Button.ContextMenu&gt; &lt;ContextMenu&gt; &lt;MenuItem IsCheckable="True" IsChecked="{Binding Path=Tag}" DataContext="{Binding Path=PlacementTarget, RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}" /&gt; &lt;/ContextMenu&gt; &lt;/Button.ContextMenu&gt; &lt;/Button&gt; &lt;/StackPanel&gt; &lt;/UserControl&gt; </code></pre> <p>Here I set the <code>UserControls</code> to a specified <code>DataTemplate</code>. The UserControls were moved out to make the XAML easier to read/navigate. In actuality the UserControls are a few hundred lines each.</p> <pre><code>&lt;Window.Resources&gt; &lt;DataTemplate x:Key"Template1"&gt; &lt;customControls:AwesomeControl/&gt; &lt;/DataTemplate&gt; ... &lt;DataTemplate x:Key"TemplateN"&gt; &lt;customControls:AwesomerControl/&gt; &lt;/DataTemplate&gt; &lt;dts:PropertyDataTemplateSelector x:Key="templateselector" Template1="{StaticResource Template1"} ... TemplateN="{StaticResource TemplateN"} &lt;/Window.Resources&gt; </code></pre> <p>The <code>ListBox</code> is defined as this.</p> <pre><code>&lt;ListBox ItemSource="{Binding Path=CollectionofMyObjects}" ItemTemplateSelector="{StaticResource templateselector}" /&gt; </code></pre> <p>I am using a single <code>ViewModel</code> to drive the <code>MainWindow</code> and the <code>UserControls</code>.</p> <p>So that's where I'm at, essentially. I have this currently working as I'd like, but in an ongoing effort to learn (this is my first MVVM/WPF/C# project) I'd like to keep exploring how to make my code "better" (however that's defined). I'm not looking to solve an error here. So to avoid a general/broad question, I'll ask what I think I want to know. Someone can correct me and I'll update the "question(s)" appropriately</p> <p><strong>Question:</strong> How can I go about producing a <code>ViewModel</code> for each of the <code>UserControls</code>? Some of the <code>ViewModels</code>, for the <code>UserControls</code>, will occasionally require two-way communication to the <code>MainWindow_ViewModel</code>. The main crux of my problem is figuring out how the multiple VMs will communicate.</p>
    singulars
    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. 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