Hi,@Zarif J Nafee .Welcome Microsoft Q&A.
I can't reproduce the problem due to incomplete code. You could try to refer to the sample below.
If you still have problems, please show me the complete code and project structure that can reproduce your problem for analysis.
project structure: (You can change the namespace according to your project structure.)
HomePage:
<UserControl.DataContext>
<local:HomePageViewModel/>
</UserControl.DataContext>
<Grid >
<TextBlock Text="{Binding Name}" Width="300" Height="50" Background="AliceBlue"/>
</Grid>
AboutPage:
<UserControl.DataContext>
<local:AboutPageViewModel/>
</UserControl.DataContext>
<Grid>
<TextBlock Text="{Binding Setting}" Background="Pink"/>
</Grid>
MainWindow.xaml:
<Window.Resources>
<DataTemplate DataType="{x:Type local:HomePageViewModel}">
<local:HomePage/>
</DataTemplate>
<DataTemplate DataType="{x:Type local:AboutPageViewModel}">
<local:AboutPage />
</DataTemplate>
</Window.Resources>
<DockPanel LastChildFill="True">
<StackPanel x:Name="navigation" DockPanel.Dock="Left">
<Button Content="Home" Command="{Binding NavigateToHomeCommand}"></Button>
<Button Content="About" Command="{Binding NavigateToAboutCommand}"></Button>
</StackPanel>
<ContentControl x:Name="Pages" DockPanel.Dock="Right" Content="{Binding CurrentViewModel}"/>
</DockPanel>
MainWindow.xaml.cs:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
DataContext=new MainWindowViewModel();
}
}
public class MainWindowViewModel : ViewModelBase
{
private object _currentViewModel;
public RelayCommand NavigateToHomeCommand { get; }
public RelayCommand NavigateToAboutCommand { get; }
public object CurrentViewModel
{
get => _currentViewModel;
set
{
_currentViewModel = value;
OnPropertyChanged("CurrentViewModel");
}
}
public MainWindowViewModel()
{
NavigateToHomeCommand = new RelayCommand(OpenHome);
NavigateToAboutCommand = new RelayCommand(OpenAbout);
}
private void OpenHome(object obj)
{
CurrentViewModel = new HomePageViewModel();
}
private void OpenAbout(object obj)
{
CurrentViewModel = new AboutPageViewModel();
}
}
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
public class HomePageViewModel : ViewModelBase { public string Name { get; set; } = "name"; }
public class AboutPageViewModel : ViewModelBase { public string Setting { get; set; } = "setting"; }
public class RelayCommand : ICommand
{
private readonly Action<object> _execute;
private readonly Predicate<object> _canExecute;
public RelayCommand(Action<object> execute)
: this(execute, null)
{
}
public RelayCommand(Action<object> execute, Predicate<object> canExecute)
{
if (execute == null)
throw new ArgumentNullException("execute");
_execute = execute;
_canExecute = canExecute;
}
public bool CanExecute(object parameter)
{
return _canExecute == null ? true : _canExecute(parameter);
}
public event EventHandler CanExecuteChanged
{
add { CommandManager.RequerySuggested += value; }
remove { CommandManager.RequerySuggested -= value; }
}
public void Execute(object parameter)
{
_execute(parameter);
}
}
The result:
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.