Let us have a scenario where we need to render any User Control in Main UserControl on the basis of items present in a list.
- Main UserControl and MainViewModel
- Child UserControl and ChildViewModel.
XAML for ChildUserControl
- <UserControl x:Class="DynaUserControlFromAnotherUsercontrolMVVM.ChildUserControl"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- mc:Ignorable="d"
- d:DesignHeight="100" d:DesignWidth="100">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition></RowDefinition>
- <RowDefinition Height="auto"></RowDefinition>
- </Grid.RowDefinitions>
- <Border x:Name="BlockService" Grid.Column="0"
- BorderThickness="0"
- BorderBrush="#406E7B"
- Padding="5"
- Height="auto" Cursor="Hand"
- Margin="0"
- CornerRadius="30">
- <Border x:Name="MainBlock" Grid.Column="0"
- BorderThickness="1"
- BorderBrush="Transparent"
- Padding="0"
- Margin="0"
- CornerRadius="30">
- <Grid>
- <Grid.RowDefinitions>
- <RowDefinition Height="auto"></RowDefinition>
- <RowDefinition></RowDefinition>
- <RowDefinition Height="auto"></RowDefinition>
- </Grid.RowDefinitions>
- <Border Name="ImageControlBlock"
- Visibility="Hidden"
- Grid.RowSpan="3" Grid.Column="0"
- BorderBrush="Transparent"
- Margin="0">
- <Image Name="imageControl" Stretch="Fill"
- />
- </Border>
- <Border x:Name="BlockInside"
- Grid.Column="0"
- VerticalAlignment="Stretch"
- BorderThickness="1"
- Grid.Row="1"
- BorderBrush="Transparent" Margin="0" CornerRadius="30">
- <StackPanel>
- <Border Name="ServiceNameBlock" Grid.Column="0" VerticalAlignment="Top" BorderThickness="1" Grid.Row="1" Opacity=".5" Background="Black"
- BorderBrush="Transparent" Margin="10" CornerRadius="10" >
- <TextBlock x:Name="LabelServiceName" Text="{Binding ServiceName}" Foreground="White" HorizontalAlignment="Center" FontSize="19" Padding="0 5"></TextBlock>
- </Border>
- </StackPanel>
- </Border>
- </Grid>
- </Border>
- </Border>
- </Grid>
- </UserControl>
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace DynaUserControlFromAnotherUsercontrolMVVM
- {
- public class ChildViewModel
- {
- private string serviceName;
- public string ServiceName
- {
- get
- {
- return serviceName;
- }
- set
- {
- if (serviceName != value)
- {
- serviceName = value;
- RaisePropertyChanged("ServiceName");
- }
- }
- }
- }
- }
- <UserControl x:Class="DynaUserControlFromAnotherUsercontrolMVVM.MainUserControl"
- xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
- xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
- xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
- xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
- mc:Ignorable="d"
- d:DesignHeight="300" d:DesignWidth="300">
- <Grid>
- </Grid>
- </UserControl>
MainViewModel

Keyur GamitPosted Jul 30, 2018, 12:25 AM
Can you explain how to change usercontrol dynamically ? i mean i have 3 usercontrol and i want to show single at a time.and if the condition change. i want to replace it by another Usercontrol.
Himani SharmaPosted Jan 25, 2018, 5:36 AM
Where to call Bind Properties method of mainviewmodel
Rohit NairPosted Jan 10, 2018, 11:19 PM
Hello,Will you please help me out in creation of ribbon groups dynamically using mvvm approach
Jasbeer SinghPosted Feb 2, 2017, 6:06 AM
Good Blog Neha, Easy to understand