Hi Jenny,
another approach is to use separate ViewModel for each page inherited from the same base class which corrosponded to the XamlWithControls.xaml.
Pages_ViewModel_base.cs in pages folder:
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.CompilerServices;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Markup;
using System.Windows.Media;
namespace WpfApp1.pages
{
abstract class Pages_ViewModel_Base : ICommand, INotifyPropertyChanged
{
public ContentControl LoadedXaml
{ get => (ContentControl)XamlReader.Load(new FileStream("pages/XamlWithControls.xaml", FileMode.Open)); }
public Brush TbCommonForeGround { get; set; } = Brushes.Black;
public string TbCommonText { get; set; }
public Visibility TbExplainVisibility { get; set; } = Visibility.Hidden;
public void Execute(object parameter)
{
switch (parameter.ToString())
{
case "Preference":
TbCommonForeGround = new SolidColorBrush(Colors.Blue);
OnPropertyChanged(nameof(TbCommonForeGround));
break;
case "ExplainOn":
TbExplainVisibility = Visibility.Visible;
OnPropertyChanged(nameof(TbExplainVisibility));
break;
case "ExplainOff":
TbExplainVisibility = Visibility.Hidden;
OnPropertyChanged(nameof(TbExplainVisibility));
break;
default:
break;
}
}
public event EventHandler CanExecuteChanged;
public bool CanExecute(object parameter) => true;
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged([CallerMemberName] string propName = "") =>
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
}
}
Page1_ViewModel.cs in in pages folder:
namespace WpfApp1.pages
{
class Page1_ViewModel : Pages_ViewModel_Base
{
protected Page1_ViewModel() { }
private static Page1_ViewModel _instance;
public static Page1_ViewModel Instance
{
get
{
if (_instance == null)
{
_instance = new Page1_ViewModel();
_instance.TbCommonText = "Introduction to Markup Languages on Page 1";
}
return _instance;
}
}
// additional properties
}
}
And in CodeBehind of each page you can instantiate different ViewModels with the same base class:
using System.Windows.Controls;
namespace WpfApp1.pages
{
/// <summary>
/// Interaction logic for Page1.xaml
/// </summary>
public partial class Page1 : Page
{
public Page1()
{
InitializeComponent();
this.DataContext = Page1_ViewModel.Instance;
}
}
}
and so on:
namespace WpfApp1.pages
{
class Page2_ViewModel : Pages_ViewModel_Base
{
protected Page2_ViewModel() { }
private static Page2_ViewModel _instance;
public static Page2_ViewModel Instance
{
get
{
if (_instance == null)
{
_instance = new Page2_ViewModel();
_instance.TbCommonText = "Introduction to Markup Languages on Page 2";
}
return _instance;
}
}
// additional properties
}
}
CodeBehind:
using System.Windows.Controls;
namespace WpfApp1.pages
{
/// <summary>
/// Interaction logic for Page2.xaml
/// </summary>
public partial class Page2 : Page
{
public Page2()
{
InitializeComponent();
this.DataContext = Page2_ViewModel.Instance;
}
}
}