Load Usercontrols in MVVM
-
Hi @all, i know that there a many articles about my problem but i cant find a solution. I am new in WPF - MVVM and i try to understand the MVVM-Logic. So i made a little project to understand that. For my later apps i want to load UserControls dynamicly to my Window. In my StartView i have a Binding to the StartViewModel. (The Binding is in the APP.xaml)
StartView app = new StartView();
StartViewModel context = new StartViewModel();the StartView
the StartViewModel
namespace test.ViewModel
{
public class StartViewModel : ViewModelBase
{
#region Fields
private UCStastistikViewModel _loadedControl;
#endregionpublic StartViewModel() { LoadedControl = new UCStastistikViewModel(); } #region Properties / Commands public UCStastistikViewModel LoadedControl { get { return \_loadedControl; } set { if (value == \_loadedControl) return; \_loadedControl = value; OnPropertyChanged("LoadedControl"); } } #endregion #region Methods #endregion }
}
UCStatistikView
-
Hi @all, i know that there a many articles about my problem but i cant find a solution. I am new in WPF - MVVM and i try to understand the MVVM-Logic. So i made a little project to understand that. For my later apps i want to load UserControls dynamicly to my Window. In my StartView i have a Binding to the StartViewModel. (The Binding is in the APP.xaml)
StartView app = new StartView();
StartViewModel context = new StartViewModel();the StartView
the StartViewModel
namespace test.ViewModel
{
public class StartViewModel : ViewModelBase
{
#region Fields
private UCStastistikViewModel _loadedControl;
#endregionpublic StartViewModel() { LoadedControl = new UCStastistikViewModel(); } #region Properties / Commands public UCStastistikViewModel LoadedControl { get { return \_loadedControl; } set { if (value == \_loadedControl) return; \_loadedControl = value; OnPropertyChanged("LoadedControl"); } } #endregion #region Methods #endregion }
}
UCStatistikView
The standard way to display a usercontrol like this is to use a DataTemplate. Here's a simple data template that you could take and adapt as you need:
<DataTemplate DataType="{vm:UCStastistikViewModel}">
<view:UCStatistikView />
</DataTemplate>Forgive your enemies - it messes with their heads
"Mind bleach! Send me mind bleach!" - Nagy Vilmos
My blog | My articles | MoXAML PowerToys | Mole 2010 - debugging made easier - my favourite utility