Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In my project I'm passing ViewModels to child windows too. I create a dependency property for the ViewModel in my child window's code behind and in the setter of this property I pass the ViewModel along to my child window's ViewModel. This means you're creating a separate ViewModel class just for your child window.</p> <p>To answer your second question, you could have your child window's ViewModel contain properties that each tab cares about, but have their data context still be the same as the child window's data context so they have access to shared properties. This is actually very easy since they automatically get the child window's data context.</p> <p>Here's an example illustrating the two concepts above.</p> <p>The child window view <strong>DetailsWindow.xaml</strong> (note that I've gotten in the habit of naming my child window views *Window.xaml instead of *View.xaml)</p> <pre><code>&lt;controls:ChildWindow x:Class="DetailsWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" xmlns:Views="clr-namespace:Views" Title="Details" DataContext="{Binding DetailsWindowViewModel, Source={StaticResource Locator}}" &gt; &lt;Grid&gt; &lt;sdk:TabControl&gt; &lt;sdk:TabItem Header="First Tab" Content="{Binding FirstTabContent}" /&gt; &lt;sdk:TabItem Header="Second Tab" Content="{Binding SecondTabContent}" /&gt; &lt;/sdk:TabControl&gt; &lt;/Grid&gt; &lt;/controls:ChildWindow&gt; </code></pre> <p>The child window view's code behind <strong>DetailsWindow.xaml.cs</strong> and its interface <strong>IDetailsWindow.cs</strong></p> <pre><code>public partial class DetailsWindow : ChildWindow, IDetailsWindow { private IDetailsWindowViewModel ViewModel { get { return this.DataContext as IDetailsWindowViewModel; } } public DetailsWindow() { InitializeComponent(); } #region Customer dependency property public const string CustomerViewModelPropertyName = "Customer"; public ICustomerViewModel Customer { get { return (ICustomerViewModel)GetValue(CustomerViewModelProperty); } set { SetValue(CustomerViewModelProperty, value); if (ViewModel != null) { ViewModel.Customer = value; } } } public static readonly DependencyProperty CustomerViewModelProperty = DependencyProperty.Register( CustomerViewModelPropertyName, typeof(ICustomerViewModel), typeof(CustomerDetailsWindow), null); #endregion } public interface IDetailsWindow { ICustomerViewModel Customer { get; set; } void Show(); } </code></pre> <p>The child window view model <strong>DetailsWindowViewModel.cs</strong> and its interface <strong>IDetailsWindowViewModel</strong></p> <pre><code>public class DetailsWindowViewModel : ViewModelBase, IDetailsWindowViewModel { public DetailsWindowViewModel(IMessenger messenger) : base(messenger) { } #region Properties #region Customer Property public const string CustomerPropertyName = "Customer"; private ICustomerViewModel _customer; public ICustomerViewModel Customer { get { return _customer; } set { if (_customer == value) return; var oldValue = _customer; _customer = value; RaisePropertyChanged(CustomerPropertyName, oldValue, value, true); } } #endregion #region FirstTabContent Property public const string FirstTabContentPropertyName = "FirstTabContent"; private FrameworkElement _firstTabContent; public FrameworkElement FirstTabContent { get { return _firstTabContent; } set { if (_firstTabContent == value) return; _firstTabContent = value; RaisePropertyChanged(FirstTabContentPropertyName); } } #endregion #region SecondTabContent Property public const string SecondTabContentPropertyName = "SecondTabContent"; private FrameworkElement _secondTabContent; public FrameworkElement SecondTabContent { get { return _secondTabContent; } set { if (_secondTabContent == value) return; _secondTabContent = value; RaisePropertyChanged(SecondTabContentPropertyName); } } #endregion #endregion } public interface IDetailsWindowViewModel { ICustomerViewModel Customer { get; set; } FrameworkElement FirstTabContent { get; set; } FrameworkElement SecondTabContent { get; set; } void Cleanup(); } </code></pre> <p>And you can show the child window from your <strong>MainPageViewModel.cs</strong> like this.</p> <pre><code>public class MainViewModel : ViewModelBase, IMainViewModel { private readonly IDetailsWindow _detailsWindow; public MainViewModel(IMessenger messenger, IDetailsWindow DetailsWindow) : base(messenger) { _detailsWindow = DetailsWindow; } private void DisplayCustomerDetails(ICustomerViewModel customerToDisplay) { _detailsWindow.Customer = customerToDisplay; _detailsWindow.Show(); } } </code></pre> <p>Note that I create interfaces for all of my view models and child windows and I use an DI/IoC container in my ViewModelLocator so that all of my ViewModels' dependencies are injected for me. You don't have to do this, but I like how it works.</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. 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