I have a mainGrid in Mainwindow. The mainGrid have a context menu. That context menu is binding to the ViewModel. When the user clicks the context menu item, new View is adding to the maingrid at runtime. Before I added the view from ViewModel when the context menu item is a click. But now only I know view type using in ViewModel is a violation. Please, anyone gives a simple example or links to refer how to add the view without from ViewModel.
<Grid x:Name="mainGrid">
<Grid.ContextMenu>
<ContextMenu>
<MenuItem Header="Add new Grid">
<i:Interaction.Triggers>
<i:EventTrigger EventName="Click">
<i:InvokeCommandAction Command="{Binding Path=AddNewGridCommand}" CommandParameter="{Binding ElementName="mainGrid"}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</MenuItem>
</ContextMenu>
</Grid.ContextMenu>
</Grid>
ViewModel
private ICommand addNewGridCommand;
public ICommand AddNewGridCommand => addNewGridCommand ?? (addNewGridCommand = new RelayCommand(addGrid, canAddGrid));
private void addGrid(object obj)
{
// Some UI like below
Grid newGrid = new Grid()
{
Height = 100, Background = Brushes.Gray
}
((obj) as Grid).Children.Add(newGrid);
}