0

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);
}
Keyur Ramoliya
  • 1,900
  • 2
  • 16
  • 17
Achu_L
  • 175
  • 1
  • 13
  • You can use the `Click` event on `MenuItem` and handle it in C# class (not the ViewModel, the _YourViewName.xaml.cs_ one). Here is an example of how to use and handle it: https://stackoverflow.com/questions/48452760/c-sharp-wpf-context-menu-item-click-event-returns-null – Marian Simonca Jul 18 '18 at 05:50
  • @Simonca, Thanks for your reply, I follow mvvm on my project. So code behind is not used. If you have any idea to add the view without from viewmodel please share. – Achu_L Jul 18 '18 at 05:55
  • MVVM states that code regarding View should not be in ViewModel class but it can be in the code behind class. If your code is independent from the ViewModel then you can write it in _.xaml.cs_ file. – Marian Simonca Jul 18 '18 at 05:58
  • See [Open File Dialog MVVM](https://stackoverflow.com/questions/1043918/open-file-dialog-mvvm/43756154#43756154) – Rekshino Jul 18 '18 at 07:15
  • Why do you need to create a `Grid` in code? Have you considered using `DataTemplate`s instead? – dymanoid Jul 18 '18 at 07:44
  • @dymanoid Thanks for your reply, For the first time empty grid is shown. When user click the context menu item then only user have to add that Grid UI dynamically at runtime. For the first time we don't have to create that Grid UI at runtime only we have to create that Grid UI – Achu_L Jul 18 '18 at 10:09
  • Just don't create any UI components in code. You tagged your question as MVVM, but I don't see any MVVM approach. Where are your view-models and data templates? – dymanoid Jul 18 '18 at 10:10

0 Answers0