-
Notifications
You must be signed in to change notification settings - Fork 263
WPF ViewModels in ATF
ATF supports WPF's Model-View-ViewModel (MVVM) pattern.
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.
- In MVVM, the view's data context is set to the view model.
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.
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.ICommand
s for OK and Cancel commands, and the CloseDialog
event.
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
, 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
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 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 inAboutDialog
. For a detailed discussion, see AboutDialog Example. -
AdaptableViewModelCollection<T, U>
: Wraps anIObservableCollection
of one type to implementIObservableCollection
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 toSce.Atf.Adaptation.BindingAdapterObjectBase
. It provides a customised set of property descriptors for WPF binding. -
ConfirmationDialogViewModel
: Interaction logic forConfirmationDialog
. -
DialogViewModelBase
: Base class for dialog view models. -
FindFileDialogViewModel
: Interaction logic for theFindFileDialog
that resolves missing files. -
FindTargetsViewModel
: Interaction logic for theFindTargetsDialog
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 inTreeViewModel
. -
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 forTargetDialog
. -
TargetViewModel
: ViewModel for a Target. Used withTargetDialog
. -
TcpIpTargetEditDialogViewModel
: View model class for theTcpIpTargetEditDialog
. -
ToolBarViewModel
: View model for an application's tool bars. Used byMainWindow
. -
TreeViewModel
: View model for a TreeView. Used inSettingsDialog
.
- ATF and WPF Overview: Overview of what ATF offers for WPF.
- WPF Application: Discuss how WPF applications differ from WinForms ones in basic application structure.
- Parallels in WPF and WinForms Support: Discusses features WPF offers that are similar to WinForms.
- WPF Application Support: Discussion of classes generally supporting applications.
- WPF Dialogs in ATF: WPF dialogs you can use.
- WPF Behaviors in ATF: ATF support for WPF behaviors.
- WPF Markup Extensions in ATF: ATF support for WPF Markup extensions.
- WPF ViewModels in ATF: Discuss WPF ViewModel support in ATF.
- Converting from WinForms to WPF: Tells how to convert a WinForms ATF-based application to WPF.
- Home
- Getting Started
- Features & Benefits
- Requirements & Dependencies
- Gallery
- Technology & Samples
- Adoption
- News
- Release Notes
- ATF Community
- Searching Documentation
- Using Documentation
- Videos
- Tutorials
- How To
- Programmer's Guide
- Reference
- Code Samples
- Documentation Files
© 2014-2015, Sony Computer Entertainment America LLC