hi
i'm trying to change ContentControl.Content at run time based on ContentControl.DataContext, so at run time i change ContentControl.DataContext Source property and i want the ContentControl.Content to be changed based on the ContentControl.DataContext Source property new value,
here is my try:
<UserControl
x:Class="wpf1.UI.View.CurrentView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-wpf1.UI.ViewModel"
xmlns:view="clr-namespace:wpf1.UI.View"
DataContext="{Binding}">
<UserControl.Resources>
<DataTemplate DataType="{x:Type vm:MyViewModel1}">
<view:MyViewModel1View DataContext="{Binding}"/>
</DataTemplate>
<DataTemplate DataType="{x:Type vm:MyViewModel2}">
<view:MyViewModel2View DataContext="{Binding}"/>
</DataTemplate>
</UserControl.Resources>
<ContentControl DataContext="{Binding}" />
</UserControl>
and here is how i use it in mainwindow
<view:CurrentView
Grid.Row="2"
DataContext="{Binding CurrentVM}"/>
and here is the mainwindow viewmodel
...
private ViewModelBase _currentVM;
public ViewModelBase CurrentVM
{
get => _currentVM;
set
{
_currentVM = value;
OnPropertyChanged(nameof(CurrentVM));
}
}
MyViewModel1 _myVM1;
MyViewModel2 _myVM2;
...
// at some point i do so
CurrentVM = _myVM1;
// at some point i do so
CurrentVM = _myVM2;
i want the CurrentView control to have only one view which matches it's datacontext.
i'm using .NetFramework 4.7 VS2019
any help will be appreciated, thanks in advance.