Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>An easy way to achieve this would be to use IoC. When you create your child view model, pass a reference of your main view model to your child viewmodel , and hold it as a private read only variable. You now access all of your main VM publics . </p> <p>An alternative solution would ppossibly be to use the Mediator pattern.</p> <p>Knocked up a simple App to demonstrate IoC solution.</p> <p>App.xaml.cs</p> <pre><code>public partial class App : Application { protected override void OnStartup(StartupEventArgs e) { base.OnStartup(e); var window = new MainWindow() {DataContext = new MainWindowViewModel() }; window.Show(); } } </code></pre> <p>MainWindowViewModel.cs</p> <pre><code>public class MainWindowViewModel : ViewModelBase { private string _statusMessage; public string StatusMessage { get { return _statusMessage; } set { _statusMessage = value; this.OnPropertyChanged("StatusMessage"); } } public ICommand OpenChildCommand { get; private set; } public MainWindowViewModel() { this.StatusMessage = "No status"; this.OpenChildCommand = new DelegateCommand((o) =&gt; this.OpenChild()); } private void OpenChild() { var window = new ChildWindow {DataContext = new ChildWindowViewModel(this)}; window.Show(); } } </code></pre> <p>ChildWindowViewModel.cs</p> <pre><code>public class ChildWindowViewModel : ViewModelBase { private readonly MainWindowViewModel _mainvm; public ChildWindowViewModel(MainWindowViewModel mainvm) { _mainvm = mainvm; this.UpdateStatusCommand = new DelegateCommand((o) =&gt; this.UpdateStatus()); } public ICommand UpdateStatusCommand { get; private set; } private void UpdateStatus() { this._mainvm.StatusMessage = "New Status"; } } </code></pre> <p>ViewModelBase.cs</p> <pre><code>public abstract class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyChanged; protected void OnPropertyChanged(string propertyName) { this.OnPropertyChanged(new PropertyChangedEventArgs(propertyName)); } protected virtual void OnPropertyChanged(PropertyChangedEventArgs e) { var handler = this.PropertyChanged; if (handler != null) { handler(this, e); } } } </code></pre> <p>DelegateCommand.cs</p> <pre><code>public class DelegateCommand : ICommand { /// &lt;summary&gt; /// Action to be performed when this command is executed /// &lt;/summary&gt; private Action&lt;object&gt; executionAction; /// &lt;summary&gt; /// Predicate to determine if the command is valid for execution /// &lt;/summary&gt; private Predicate&lt;object&gt; canExecutePredicate; /// &lt;summary&gt; /// Initializes a new instance of the DelegateCommand class. /// The command will always be valid for execution. /// &lt;/summary&gt; /// &lt;param name="execute"&gt;The delegate to call on execution&lt;/param&gt; public DelegateCommand(Action&lt;object&gt; execute) : this(execute, null) { } /// &lt;summary&gt; /// Initializes a new instance of the DelegateCommand class. /// &lt;/summary&gt; /// &lt;param name="execute"&gt;The delegate to call on execution&lt;/param&gt; /// &lt;param name="canExecute"&gt;The predicate to determine if command is valid for execution&lt;/param&gt; public DelegateCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute) { if (execute == null) { throw new ArgumentNullException("execute"); } this.executionAction = execute; this.canExecutePredicate = canExecute; } /// &lt;summary&gt; /// Raised when CanExecute is changed /// &lt;/summary&gt; public event EventHandler CanExecuteChanged { add { CommandManager.RequerySuggested += value; } remove { CommandManager.RequerySuggested -= value; } } /// &lt;summary&gt; /// Executes the delegate backing this DelegateCommand /// &lt;/summary&gt; /// &lt;param name="parameter"&gt;parameter to pass to predicate&lt;/param&gt; /// &lt;returns&gt;True if command is valid for execution&lt;/returns&gt; public bool CanExecute(object parameter) { return this.canExecutePredicate == null ? true : this.canExecutePredicate(parameter); } /// &lt;summary&gt; /// Executes the delegate backing this DelegateCommand /// &lt;/summary&gt; /// &lt;param name="parameter"&gt;parameter to pass to delegate&lt;/param&gt; /// &lt;exception cref="InvalidOperationException"&gt;Thrown if CanExecute returns false&lt;/exception&gt; public void Execute(object parameter) { if (!this.CanExecute(parameter)) { throw new InvalidOperationException("The command is not valid for execution, check the CanExecute method before attempting to execute."); } this.executionAction(parameter); } } </code></pre> <p>MainWindow.xaml</p> <pre><code>&lt;Window x:Class="WpfApplication2.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"&gt; &lt;StackPanel&gt; &lt;TextBlock Text="{Binding StatusMessage, Mode=OneWay}" /&gt; &lt;Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="Open Child Window" Command="{Binding Path=OpenChildCommand}"/&gt; &lt;/StackPanel&gt; </code></pre> <p></p> <p>ChildWindow.xaml</p> <pre><code>&lt;Window x:Class="WpfApplication2.ChildWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ChildWindow" Height="300" Width="300"&gt; &lt;Grid&gt; &lt;Button HorizontalAlignment="Center" VerticalAlignment="Center" Content="UpdateStatus" Command="{Binding Path=UpdateStatusCommand}" /&gt; &lt;/Grid&gt; </code></pre> <p></p> <p>Image before clicking on update status</p> <p><img src="https://i.stack.imgur.com/rrjB6.png" alt="Before"></p> <p>Image after clicking on updatestatus</p> <p><img src="https://i.stack.imgur.com/ZQ5JG.png" alt="enter image description here"></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload