Search code examples
wpfmvvmdatatemplate

Displaying different instances of UserControl through DataTemplate


<UserControl x:Name="AView" .... />
public class AViewModel { .. }

<UserControl x:Name="ParentView">
  <UserControl.Resources>
    <DataTemplate DataType={x:Type vm:AViewModel}>
      <vw:AView />
    </DataTemplate>
  </UserControl.Resources>

  <Grid.ColumnDefinitions>
    <ColumnDefinition />
    <ColumnDefinition />
  </Grid.ColumnDefinitions>

  <ContentControl Content="{Binding PropertyA}" />
  <ContentControl Content="{Binding PropertyB}" Grid.Column="1" />
</UserControl

public class ParentViewModel
{
    public AViewModel PropertyA {get;set;}
    public AViewModel PropertyB {get;set;}
}

PropertyA and PropertyB hold references to different instances of AViewModel.

Q: How can I display different instances of AView in ContentControls that are bound to PropertyA and PropertyB?


Solution

  • The XAML you posted will create 2 instances of AView inside each ContentControl.