Since you already have an answer using code-Behind, I will give a solution using MVVM.
The property you want to fill is called "content". We must connect it to a Viewmodel property that will represent Usercontrol. First let’s create a Viewmodel:
public class ViewModel : INotifyPropertyChanged
{
//Implementacao do INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged;
private UserControl controle;
public UserControl Controle
{
get { return controle; }
set
{
controle = value;
OnPropertyChanged("Controle");
}
}
public List<UserControlModel> Controle { get; set; } //Lista dos UserControls
public Command<string> MudarControle { get; set; }
public ViewModel()
{
Controles = new List<UserControlModel>();
//Preencher lista aqui;
MudarControle = new Command<string>(Alterar);
}
protected void Alterar(string UserControl)
{
//Simplifiquei, mas aqui vai uns testes para saber se o controle existe mesmo na lista
Controle = Controles.FirstOrDefault(c => c.Nome == UserControl);
}
}
Now we create our model:
public class UserControlModel
{
public string Nome { get; set; }
public UserControl Controle { get; set; }
public UserControlModel() { }
public UserControlModel(string nome, UserControl user)
{
Nome = nome;
Controle = user;
}
}
And now just turn on the bindings:
In your Mainwindow class:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext = new ViewModel();
}
}
And the shampoo:
<ContentControl Content="{Binding Controle.Controle}"/>
Note that here is the menu button that will fill Contentcontrol
<Button Content="Item1" Height="50" Width="100" Command="{Binding MudarControle}" CommandParameter="Item1"/>
Here is a very simplistic example where the buttons are already preset in the menu, but changing this code for both sides of the grid to change according to the context is quite simple. It follows the same mechanics. If you are in doubt about how to implement the command class, take a look at this link:
https://guiadosprogramadores.wordpress.com/2016/12/21/mvvm-e-seus-commands/
There are several ways to do this. Are you using the MVVM standard? Use any framework to help implement it?
– ramaral
I tried to implement the MVVM standard but I didn’t find any easy tutorial.. I’m just starting out with this language, I had a hard time understanding. If you know of any simple tutorial let me know.. kkkk
– Dênis Azevedo