Reputation: 319
I have a UWP application that will manage 3 entities (Category, Plate, Menu). I have a viewmodel for each entity and a list of viewmodel to choose the entity to manage. So I would like that when I choose a entity in viewmodel's list in the page is load dinamically the usercontrol.
I try to contentcontrol but it is not working.
my xaml code are:
<ListBox Grid.Row="1" ItemsSource="{Binding ConfigurationItems}"
SelectedItem="{Binding ConfigurationItemSelected, Mode=TwoWay}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=ConfigurationAbstract}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<StackPanel Grid.Row="1" Grid.Column="1" Margin="10,0,0,0">
<TextBlock Text="{Binding Path=ConfigurationItemSelected.ConfigurationAbstract}" Style="{StaticResource TitleTextBlockStyle}"/>
<Grid Grid.Row="1" Grid.Column="1"
Visibility="{Binding ImportVisible, Converter={StaticResource b2v} }"
>
<Grid.RowDefinitions>
<RowDefinition Height="40"></RowDefinition>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="75*"></ColumnDefinition>
<ColumnDefinition Width="25*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<StackPanel Grid.Column="0" Orientation="Horizontal" VerticalAlignment="Center">
<TextBlock Text="File Importato: "/>
<TextBlock Text="{Binding Path= ConfigurationItemSelected.ImportedFilePathName}"/>
</StackPanel>
<StackPanel Grid.Column="1" Orientation="Horizontal">
<Button Content="Scegli..." Command="{Binding Path=ConfigurationItemSelected.ChooseCommand}" Margin="5,0"></Button>
<Button Content="Importa" Command="{Binding Path=ConfigurationItemSelected.ImportCommand}" Margin="5,0"></Button>
</StackPanel>
</Grid>
<ContentControl Content="{Binding Path=ConfigurationItemSelected}"/>
</StackPanel>
Thank you Regards
I have update my code with:
<Page.Resources>
<DataTemplate x:DataType="vm:CategoriesViewModel" x:Key="Categorie">
<controls:UcCategories DataContext="{Binding Path=ConfigurationItemSelected}" />
</DataTemplate>
<DataTemplate x:DataType="vm:MenuItemsViewModel" x:Key="Menu">
<controls:UcMenu DataContext="{Binding Path=ConfigurationItemSelected}" />
</DataTemplate>
<DataTemplate x:DataType="vm:PlatesViewModel" x:Key="Piatti">
<controls:UcPlates DataContext="{Binding Path=ConfigurationItemSelected}" />
</DataTemplate>
</Page.Resources>
<ContentControl Content="{Binding Path=ConfigurationItemSelected}">
</ContentControl>
But not working again...
Upvotes: 1
Views: 790
Reputation: 1565
You have to create a DataTemplateSelector
where you can decide which template applied to the ContentControl, depend on some logic, (for now the data's type).
Selector:
public class MyDataTemplateSelector : DataTemplateSelector
{
public DataTemplate CategoryTemplate { get; set; }
public DataTemplate MenuTemplate { get; set; }
public DataTemplate PlateTemplate { get; set; }
protected override DataTemplate SelectTemplateCore(object item, DependencyObject container)
{
if (item is CategoriesViewModel)
{
return CategoryTemplate;
}
else if (item is MenuItemsViewModel)
{
return MenuTemplate;
}
else if (item is PlatesViewModel)
{
return PlateTemplate;
}
else
{
return base.SelectTemplateCore(item);
}
}
}
XAML:
<Page.Resources>
<DataTemplate x:DataType="vm:CategoriesViewModel" x:Key="Categorie">
<controls:UcCategories DataContext="{Binding Path=ConfigurationItemSelected}" />
</DataTemplate>
<DataTemplate x:DataType="vm:MenuItemsViewModel" x:Key="Menu">
<controls:UcMenu DataContext="{Binding Path=ConfigurationItemSelected}" />
</DataTemplate>
<DataTemplate x:DataType="vm:PlatesViewModel" x:Key="Piatti">
<controls:UcPlates DataContext="{Binding Path=ConfigurationItemSelected}" />
</DataTemplate>
<local:MyDataTemplateSelector x:Key="TemplateSelector"
CategoryTemplate="{StaticResource Categorie}"
MenuTemplate="{StaticResource Menu}"
PlateTemplate="{StaticResource Piatti}"/>
</Page.Resources>
<ContentControl Content="{Binding Path=ConfigurationItemSelected}"
ContentTemplateSelector="{StaticResource TemplateSelector}"/>
Upvotes: 1