c # – How do I change the contents of my MainWindow when I click on a button inside my UserControl?

I just started learning MVVM and I want to know what would be the best practice when I want to change the contents of my MainWindow when I click on a button inside my UserControl.

This is the UserControl that contains the buttons that I will click on. When I click on the IconButtons that this produces, I want to be able to change MainGrid to a grid that contains the information of my IconModel.

To clarify, I would like the Grid to show the Name, the Description, the User Name, the Password of the Icon and a button that opens a program or a website using the Route. The Icon class is inside IconModel.cs which is published below.

IconView.xaml

    
    
        
    
    
        
            
                
                    
                
            
        
    

MainWindow.xaml

    
    
        
            
            
        
        

        
    

For reference, here are my IconModel and IconViewModel classes.

IconModel.cs

using the system;
using System.Collections.Generic;
using System.ComponentModel;
using System.Xml.Serialization;

ProgramManager.Model namespace
{

Public class IconModel {}

    [XmlRoot(ElementName = "Icons", IsNullable = true, DataType = "string")]
    [XmlType(AnonymousType = true)]
    [XmlInclude(typeof(ProgramIcon))]
    [XmlInclude(typeof(WebsiteIcon))]
    
    
    
    Public class icon: IComparableIComparer, INotifyPropertyChanged
{
private chain name;
description of the private chain;
private chain road;
private chain username;
private chain password;

        [XmlElement("Name")]
        Name of the public chain
{
get {return name; }
set
{
yes (name! = value)
{
name = value;
RaisePropertyChanged ("Name");
}
}
}

        [XmlElement("Description")]
        Public chain Description
{
get {return description; }
set
{
yes (description! = value)
{
description = value;
RaisePropertyChanged ("Description");
}
}
}

        [XmlElement("Path")]
        public chain route
{
get {return route; }
set
{
if (path! = value)
{
path = value;
RaisePropertyChanged ("Path");
}
}
}

        [XmlElement("Username")]
        public string username
{
get {return username; }
set
{
yes (username! = value)
{
username = value;
RaisePropertyChanged ("Username");
}
}
}

        [XmlElement("Password")]
        public string password
{
get {return password; }
set
{
yes (password! = value)
{
password = value;
RaisePropertyChanged ("Password");
}
}
}

initial public string {
obtain
{
returns this.Name.Substring (0, 1) .ToUpper ();
}
}

public event PropertyChangedEventHandler PropertyChanged;

Private void RaisePropertyChanged (string property)
{
if (PropertyChanged! = null)
{
PropertyChanged (this, new PropertyChangedEventArgs (property));
}
}

Compare public int (Icon x, Icon y)
{
return (x.Equals (null) || y.Equals (null))? 0: x.CompareTo (y);
}

public int CompareTo (icon other)
{
return string.Compare (this.Name, other.Name);
}
}

public class ProgramIcon: icon {}
public class WebsiteIcon: Icon {}
}

IconViewModel.cs

using ProgramManager.Helpers;
using ProgramManager.Model;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;

ProgramManager.ViewModel namespace
{
Public class IconViewModel: INotifyPropertyChanged
{
Static read-only static list list of icons
Private ObservableCollection icons;
Public ObservableCollection Icons
{
get {return icons; }
set
{
yes (icons! = value)
{
icons = value;
RaisePropertyChanged ("Icons");
}
}
}
public event PropertyChangedEventHandler PropertyChanged;

Private void RaisePropertyChanged (string property)
{
if (PropertyChanged! = null)
{
PropertyChanged (this, new PropertyChangedEventArgs (property));
}
}

Public IconViewModel ()
{
LoadIcons ();
}

Static IconViewModel ()
{
if (! Directory.Exists (Constants.ResourcesDirectory))
{
Directory.CreateDirectory (Constants.ResourcesDirectory);
}

iconsList = XmlDataStorage.GetList(Constants.IconsFilePath);
}

empty public LoadIcons ()
{
ObservableCollection icons = new ObservableCollection();

foreach (var icon in iconsList)
{
icons.Add (icon);
}

Icons = icons;
}

Public void AddIcon (icon icon)
{
iconsList.Add (icon);
iconsList.Sort ();

XmlDataStorage.SaveToXml (Constants.IconsFilePath, iconsList);

LoadIcons ();
}
}
}

I have tried to access the IconButton in MainWindow from IconView.xaml, but it seems that I can not find a way to do it.

Any help is appreciated, thanks!

How can I close a usercontrol and open another with the same button?

Type so I have a form composed of the 6 buttons and a panel. I want that when I click on the record button that opens usercontrol1 and if I click on the query button closes first usercontrol1 and then opens usercontrol2 …

c # – Store values ​​in variables when changing UserControl

Good day,

I am working on an application which is a form, I have worked on C # using Windows Forms and User Controls.

Here is an example:

enter the description of the image here

This acts as the Windows Form Main, in which the center is a panel where you link the User Controls:

enter the description of the image here

When running the program is as follows:

enter the description of the image here

Pressing the next arrow launches another different form in another User Control.

My question is how I can store the values ​​of textbox, datetimepicker, etc, variables and then store them in a database, what happens is that I can not save them by pressing the button of the date since one is the User Control and another It's Windows Forms and for obvious reasons you can not. I would like to know if there is a User Control event like changing it to do such a thing or something.

Greetings I hope I could explain.