Skip to content

WPF ViewModels in ATF

Gary edited this page Mar 2, 2015 · 2 revisions

ATF supports WPF's Model-View-ViewModel (MVVM) pattern.

Table of Contents

View Model Basics

The article Implementing the MVVM Pattern Using the Prism Library 5.0 for WPF contains some interesting WPF model insights:

The view model implements properties and commands to which the view can data bind and notifies the view of any state changes through change notification events.
Here's another observation, this one regarding data binding:




In MVVM, the view's data context is set to the view model.
For example, the view can be a control, which as a FrameworkElement, has a DataContext dependency property. This property is set the a view model. The DialogUtils class has several methods that set DataContext when the dialog is instantiated and displayed, as in ShowDialogWithViewModel<TDialog, TViewModel>():
public static bool? ShowDialogWithViewModel<TDialog, TViewModel>()
	where TDialog : Window
{
	TDialog dialog = Activator.CreateInstance<TDialog>();
	dialog.DataContext = Activator.CreateInstance<TViewModel>();
	return dialog.ShowParentedDialog();
}

For more information on using the DataContext dependency property, see Data Binding.

ATF Base ViewModel Support

NotifyPropertyChangedBase is the base class for all view model classes in ATF. It implements INotifyPropertyChanged. This class revolves around managing the INotifyPropertyChanged.PropertyChanged event.

DialogViewModelBase is the base class for dialog view model classes. It derives from NotifyPropertyChangedBase and implements IDialogViewModel. It implements commands and logic for OK and Cancel buttons and validation.

IDialogViewModel is the base interface for dialog view models. It defines properties commonly used in dialogs: Title, System.Windows.Input.ICommands for OK and Cancel commands, and the CloseDialog event.

AboutDialog Example

The AboutDialog class provides a simple example of how ATF uses MVVM in WPF. This class represents an application's About dialog. AboutDialog is used by ATF Simple DOM Editor WPF Sample.

AboutDialog Class

AboutDialog, defined in AboutDialog.xaml, is the view class part. It derives from Sce.Atf.Wpf.Controls.CommonDialog, a normal common dialog which deals with view model binding directly. CommonDialog ultimately derives from System.Windows.Controls.Control.

AboutDialog.xaml sets up several data bindings, as in the section describing the StackPanel:

...
<StackPanel>
	<StackPanel>
		<Image Source="{Binding Banner, Mode=OneTime}" Margin="0,0,0,4"/>
		<StackPanel Orientation="Horizontal" Margin="0,0,0,4">
			<Label Content="{l:Loc Version:}" Margin="0,0,4,0"/>
			<Label Content="{Binding Mode=OneTime, Path=Version}" />
			<Label Content="{l:Loc Build:}" Margin="4,0,4,0"/>
			<Label Content="{Binding Mode=OneTime, Path=FileVersion}" />
		</StackPanel>
		<Label Content="{Binding Mode=OneTime, Path=Copyright}"  />
		<Label Content="{Binding Mode=OneTime, Path=Company}" />
		...

For example, the path for am image comes from the binding to the Banner property. Similarly, data for labels comes from the Version and FileVersion properties.

In AboutDialog.xaml.cs, the Button_Click() method specifies the action when the Copy Info button is clicked on the About dialog:

private void Button_Click(object sender, System.Windows.RoutedEventArgs e)
{
	var vm = DataContext as AboutDialogViewModel;
	if (vm != null)
	{
		var sb = new StringBuilder();
		sb.AppendLine(vm.ProductTitle);
		sb.AppendLine(vm.Version);
		foreach (var assembly in vm.Assemblies)
			sb.AppendLine(assembly);
		
		Clipboard.SetDataObject(sb.ToString());
	}
}

The first line references the DataContext dependency property of AboutDialog. Note that it is cast to AboutDialogViewModel, the view model for AboutDialog, because that's what resides in DataContext. When the About dialog displays, AboutDialog is set to a AboutDialogViewModel instance by the DialogUtils method mentioned in View Model Basics.

AboutDialogViewModel

AboutDialogViewModel is the view model class and derives from DialogViewModelBase.

The main purpose of AboutDialogViewModel is to define a set of properties that AboutDialog can bind to, as shown in the AboutDialog.xaml file seen in AboutDialog Class. This allows AboutDialog to display information about the application.

In this simple MVVM illustration, AboutDialogViewModel also serves as the Model part of the MVVM. Instead of getting data from a model class, AboutDialogViewModel obtains data from internal sources, as in the Version property:

public string Version
{
	get
	{
		return AtfVersion.GetVersion().ToString();
	}
}

Note that the Version property was referenced in a binding, as seen in the AboutDialog.xaml above.

And here is Description:

public string Description
{
	get { return CalculatePropertyValue<AssemblyDescriptionAttribute>(m_propertyNameDescription, m_xPathDescription); }
}

CalculatePropertyValue() is a private AboutDialogViewModel utility function that gets the specified property value either from a specific attribute, or from a resource dictionary.

ATF ViewModel Classes

ATF has ViewModel classes to provide for a variety of common application needs in the Sce.Atf.Wpf.Models namespace. Here are typical ones, many of which are used with ATF's WPF controls.

  • AboutDialogViewModel: About dialog, used in AboutDialog. For a detailed discussion, see AboutDialog Example.
  • AdaptableViewModelCollection<T, U>: Wraps an IObservableCollection of one type to implement IObservableCollection of another type. Useful for WPF MVVM situations where a collection must be adapted to a new collection of unique view models rather than shared view models.
  • BindingAdapterObjectBase: Base class for binding adapter objects, analogous to Sce.Atf.Adaptation.BindingAdapterObjectBase. It provides a customised set of property descriptors for WPF binding.
  • ConfirmationDialogViewModel: Interaction logic for ConfirmationDialog.
  • DialogViewModelBase: Base class for dialog view models.
  • FindFileDialogViewModel: Interaction logic for the FindFileDialog that resolves missing files.
  • FindTargetsViewModel: Interaction logic for the FindTargetsDialog that finds targets.
  • MainMenuViewModel: View model for an application's menus.
  • MenuModel: Menu View Model. Used for sub menus and as a base class for root menus.
  • Node: View model for a node in TreeViewModel.
  • NotifyPropertyChangedBase: Base class for all view models.
  • OutputItemVm: View model for items in an output window.
  • OutputVm: View model for an output window.
  • ProgressViewModel: View model used for both progress dialog and for progress status item.
  • StatusBarViewModel: View model used for status bar.
  • TargetDialogViewModel: View model for TargetDialog.
  • TargetViewModel: ViewModel for a Target. Used with TargetDialog.
  • TcpIpTargetEditDialogViewModel: View model class for the TcpIpTargetEditDialog.
  • ToolBarViewModel: View model for an application's tool bars. Used by MainWindow.
  • TreeViewModel: View model for a TreeView. Used in SettingsDialog.

Topics in this Section

Clone this wiki locally