Hello,
I am trying to build a WPF Application and want to open a UserControl2 from inside of the UserControl1 by clicking a button inside of the UserControl1.
The UserControl1 = searchResultsUserControl() (in a Grid of the MainWindow) displays the content of a Database in a DataGrid and in each line is a button to Update/edit the selected line.
After clicking this edit button the UserControl1 should close and the UserControl2 = editUserControl()(the edit mask for the selected entry) should open at the same place the UserControl2 was.

I tryed this with a unsuccessful with Children.Add in the MainWindow, but here is the ploblem that I can not close the UserControl1 from inside the UserControl1 and cannot open up the UserControl2 from there...


I also tryed it with a ViewModel with a Command Binding at the Buttons, but this doesnt work inside the DataGridTemplateColumn.CellTemplate in the DateGrid where the Buttons are located...

UserControl1 (XAML):
Code:
<DataGrid>
........
    <DataGridTemplateColumn>
                    <DataGridTemplateColumn.HeaderTemplate>
                        <DataTemplate>
                            <TextBlock Text="Action" />
                        </DataTemplate>
                    </DataGridTemplateColumn.HeaderTemplate>
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                     <Button x:Name="updateBtn" Content="Update" Command="{Binding Cmd}" CommandParameter="ViewEditUserControl"  />
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
     </DataGridTemplateColumn>
</DataGrid>
ViewModel (C#):
Code:
.....
public UserControl Detail { get; set; }

        public ICommand Cmd { get => new RelayCommand(CmdExec); } // property for binding Button
        private void CmdExec(object obj) // execute when clicking Button
        {
            
            Console.WriteLine(obj.ToString());
            switch (obj.ToString())
            {
                case "ViewSQLContent":
                    Detail = new searchResultsUserControl();
                    break;
                case "ViewEditUserControl":
                    Console.WriteLine("test");
                    Detail = new editUserControl();

                    break;
                case "SaveChanges":
                    Detail = new searchResultsUserControl();
                    break;
                    
                default:
                    break;
            .....

I would really appeciate when somebody can help me with this problem or give me some advise how to do this in the easiest way. (;

Best Regards