Note that there are some explanatory texts on larger screens.

plurals
  1. POIncluding partial views when applying the Mode-View-ViewModel design pattern
    primarykey
    data
    text
    <p>Consider that I have an application that just handles <code>Messages</code> and <code>Users</code> I want my Window to have a common <code>Menu</code> and an area where the current <code>View</code> is displayed.</p> <p>I can only work with either Messages or Users so I cannot work simultaniously with both Views. Therefore I have the following <strong>Controls</strong></p> <ul> <li>MessageView.xaml</li> <li>UserView.xaml</li> </ul> <p>Just to make it a bit easier, both the <code>Message Model</code> and the <code>User Model</code> looks like this:</p> <ul> <li>Name</li> <li>Description</li> </ul> <p>Now, I have the following three ViewModels:</p> <ul> <li>MainWindowViewModel</li> <li>UsersViewModel</li> <li>MessagesViewModel</li> </ul> <p>The <code>UsersViewModel</code> and the <code>MessagesViewModel</code> both just fetch an <code>ObserverableCollection&lt;T&gt;</code> of its regarding <code>Model</code> which is bound in the corresponding <code>View</code> like this:</p> <p><code>&lt;DataGrid ItemSource="{Binding ModelCollection}" /&gt;</code></p> <p>The <code>MainWindowViewModel</code> hooks up two different <code>Commands</code> that have implemented <code>ICommand</code> that looks something like the following:</p> <pre><code>public class ShowMessagesCommand : ICommand { private ViewModelBase ViewModel { get; set; } public ShowMessagesCommand (ViewModelBase viewModel) { ViewModel = viewModel; } public void Execute(object parameter) { var viewModel = new ProductsViewModel(); ViewModel.PartialViewModel = new MessageView { DataContext = viewModel }; } public bool CanExecute(object parameter) { return true; } public event EventHandler CanExecuteChanged; } </code></pre> <p>And there is another one a like it that will show Users. Now this introduced <code>ViewModelBase</code> which only holds the following:</p> <pre><code> public UIElement PartialViewModel { get { return (UIElement)GetValue(PartialViewModelProperty); } set { SetValue(PartialViewModelProperty, value); } } public static readonly DependencyProperty PartialViewModelProperty = DependencyProperty.Register("PartialViewModel", typeof(UIElement), typeof(ViewModelBase), new UIPropertyMetadata(null)); </code></pre> <p>This dependency property is used in the <code>MainWindow.xaml</code> to display the <code>User Control</code> dynamicly like this:</p> <p><code>&lt;UserControl Content="{Binding PartialViewModel}" /&gt;</code></p> <p>There are also two buttons on this <code>Window</code> that fires the Commands:</p> <ul> <li>ShowMessagesCommand</li> <li>ShowUsersCommand</li> </ul> <p>And when these are fired, the UserControl changes because <code>PartialViewModel</code> is a dependency property.</p> <p>I want to know <strong>if this is bad practice?</strong> Should I not inject the User Control like this? Is there another "better" alternative that corresponds better with the design pattern? Or is this a nice way of including partial views?</p>
    singulars
    1. This table or related slice is empty.
    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