Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:dotnet:wpf:implementing_mvvm [2010/10/18 14:44] smayr |
swdev:dotnet:wpf:implementing_mvvm [2013/03/12 17:57] (current) smayr [Resources] |
||
---|---|---|---|
Line 5: | Line 5: | ||
* ViewModel: Models of the data as " | * ViewModel: Models of the data as " | ||
* View: Data binding to ViewModel, with little " | * View: Data binding to ViewModel, with little " | ||
+ | |||
+ | === Model === | ||
+ | |||
+ | === View Model === | ||
+ | |||
+ | <code csharp> | ||
+ | // | ||
+ | // Class TAppViewModel | ||
+ | // | ||
+ | public class TAppViewModel : INotifyPropertyChanged | ||
+ | { | ||
+ | #region Fields, Events, and Properties | ||
+ | // | ||
+ | // Fields | ||
+ | // | ||
+ | private string m_ApplicationName | ||
+ | private string m_ApplicationVersion | ||
+ | private string m_ApplicationBuildNumber = " | ||
+ | private string m_ApplicationDevStage | ||
+ | private int m_Tag = 0; | ||
+ | |||
+ | // | ||
+ | // Events | ||
+ | // | ||
+ | public event PropertyChangedEventHandler PropertyChanged; | ||
+ | | ||
+ | // | ||
+ | // Properties | ||
+ | // | ||
+ | public string ApplicationName | ||
+ | { | ||
+ | get { return m_ApplicationName; | ||
+ | set { m_ApplicationName = value; } | ||
+ | } | ||
+ | public string ApplicationVersion | ||
+ | { | ||
+ | get { return m_ApplicationVersion; | ||
+ | set { m_ApplicationVersion = value; } | ||
+ | } | ||
+ | public string ApplicationBuildNumber | ||
+ | { | ||
+ | get { return m_ApplicationBuildNumber; | ||
+ | set { m_ApplicationBuildNumber = value; } | ||
+ | } | ||
+ | ... | ||
+ | | ||
+ | public TAppViewModel() | ||
+ | { | ||
+ | } | ||
+ | | ||
+ | public void SomeWorkToDo() | ||
+ | { | ||
+ | // Trigger Change | ||
+ | if (PropertyChanged != null) | ||
+ | { | ||
+ | TAppLog.LogMessage(" | ||
+ | PropertyChanged(this, | ||
+ | PropertyChanged(this, | ||
+ | } | ||
+ | } | ||
+ | | ||
+ | } | ||
+ | </ | ||
+ | |||
=== Main View === | === Main View === | ||
The '' | The '' | ||
Line 27: | Line 91: | ||
</< | </< | ||
</ | </ | ||
- | === User Controls === | + | |
- | User Controls in '' | + | === User Controls |
+ | |||
+ | User Controls in '' | ||
+ | |||
+ | For example, in the '' | ||
<code xml> | <code xml> | ||
< | < | ||
< | < | ||
< | < | ||
- | < | + | < |
</ | </ | ||
</ | </ | ||
< | < | ||
< | < | ||
- | < | + | < |
</ | </ | ||
</ | </ | ||
< | < | ||
< | < | ||
- | < | + | < |
</ | </ | ||
</ | </ | ||
Line 49: | Line 117: | ||
</ | </ | ||
- | The dependency property '' | + | The user control |
<code csharp> | <code csharp> | ||
public class ucUserControlTriangle : UserControl | public class ucUserControlTriangle : UserControl | ||
{ | { | ||
+ | #region AppViewModel dependency property implementation | ||
/// | /// | ||
- | /// Dependency Properties | + | /// App View Model |
/// | /// | ||
Line 78: | Line 147: | ||
get { return (TAppViewModel)GetValue(AppViewModelProperty); | get { return (TAppViewModel)GetValue(AppViewModelProperty); | ||
set { SetValue(AppViewModelProperty, | set { SetValue(AppViewModelProperty, | ||
- | |||
- | |||
- | /// | ||
- | /// Constructor | ||
- | /// | ||
- | public ucUserControlTriangle() | ||
- | { | ||
- | InitializeComponents(); | ||
- | | ||
- | // Set DataContext | ||
- | this.DataContext = AppViewModel; | ||
} | } | ||
- | | + | |
/// | /// | ||
/// < | /// < | ||
- | /// | + | /// OnPropertyChanged event handler for app view model. |
/// </ | /// </ | ||
- | /// <param name=" | + | /// <param name=" |
- | /// <param name=" | + | /// <param name=" |
/// | /// | ||
private static void OnAppViewModelPropertyChanged( | private static void OnAppViewModelPropertyChanged( | ||
Line 137: | Line 195: | ||
return data is TAppViewModel; | return data is TAppViewModel; | ||
} | } | ||
+ | | ||
+ | #endregion | ||
| | ||
+ | ... | ||
+ | | ||
+ | /// | ||
+ | /// Constructor | ||
+ | /// | ||
+ | public ucUserControlTriangle() | ||
+ | { | ||
+ | InitializeComponents(); | ||
+ | | ||
+ | // Set DataContext | ||
+ | this.DataContext = AppViewModel; | ||
+ | } | ||
+ | |||
... | ... | ||
| | ||
Line 164: | Line 237: | ||
... | ... | ||
xmlns: | xmlns: | ||
- | | ||
- | < | ||
- | < | ||
- | </< | ||
... | ... | ||
<!-- < | <!-- < | ||
Line 180: | Line 249: | ||
</ | </ | ||
</ | </ | ||
+ | |||
+ | == Resources == | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | |||
+ |