How to update view from viewmodel in WPF using MVVM design

I am using WFP with MVVM design for my first project, and I am having a problem updating the view after processing the command from the client to update the object. At this time, the view can talk to the viewmodel, but the viewmodel could not return to viewing. Does anyone know how to do this? Thanks, Jdang

+7
source share
4 answers

You must force the ViewModel to inject INotifyPropertyChanged and fire the PropertyChanged event when the ViewModel properties change. Assuming the user interface is tied to ViewModel properties, this should work.

+3
source

If you are new to the MVVM template, I recommend the following as excellent resources from MSDN, which cover both the template and its implementation in WPF and Silverlight applications:

Based on what you said, it looks like you will want to see the data binding and how you can use INotifyPropertyChanged , INotifyCollectionChanged and ICollectionView to provide two-way communication between your views and view models.

Data Binding Silverlight and WPF support several data binding modes. Thanks to one-way data binding, user interface controls can be snapped to the view model so that they display the value of the underlying data when rendering. Two-way data binding also automatically updates the underlying data when the user modifies it in the user interface. To ensure that the user interface is updated when data changes in the presentation model, it must implement an appropriate change notification interface.

+3
source

In a typical MVVM application, you use bindings to connect the view to the ViewModel. Bindings are automatically updated when the ViewModel raises the PropertyChanged event defined by the INotifyPropertyChanged interface. Therefore, you need to implement this interface in the ViewModel and raise the PropertyChanged event when the property value is changed and the view automatically reflects the change.

+2
source

In addition to the other answers, I would also suggest that your ViewModels extend DependencyObject .

Some people think that DependencyObjects are a lot of weight (and they can be if you create thousands of instances of them) and a little complicated for new users (there are most definitely situations where this is true). However, there are other benefits to DependencyObjects, such as automatic support for property change notifications and the speed of binding evaluations.

Here is my DP fragment (save as DependencyProperty.snippet in C: \ Users [YOUR NAME HERE] \ Documents \ Visual Studio [2010, 2008] \ Code Snippets \ Visual C # \ My Code Snippets):

 <?xml version="1.0" encoding="utf-8"?> <CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet"> <CodeSnippet Format="1.0.0"> <Header> <SnippetTypes> <SnippetType>Expansion</SnippetType> </SnippetTypes> <Title>SnippetFile1</Title> <Author>will</Author> <Description> </Description> <HelpUrl> </HelpUrl> <Shortcut>dp</Shortcut> </Header> <Snippet> <Declarations> <Literal Editable="true"> <ID>PropertyName</ID> <ToolTip>Property name</ToolTip> <Default>PropertyName</Default> <Function> </Function> </Literal> <Literal Editable="false"> <ID>ClassName</ID> <ToolTip>Class name</ToolTip> <Default>ClassName</Default> <Function>ClassName()</Function> </Literal> <Literal Editable="true"> <ID>Type</ID> <ToolTip>Property type</ToolTip> <Default>object</Default> <Function> </Function> </Literal> <Literal Editable="true"> <ID>DefaultValue</ID> <ToolTip>Default value</ToolTip> <Default>null</Default> <Function> </Function> </Literal> </Declarations> <Code Language="csharp"><![CDATA[#region $PropertyName$ /// <summary> /// The <see cref="DependencyProperty"/> for <see cref="$PropertyName$"/>. /// </summary> public static readonly DependencyProperty $PropertyName$Property = DependencyProperty.Register( $PropertyName$Name, typeof($Type$), typeof($ClassName$), new UIPropertyMetadata($DefaultValue$)); /// <summary> /// The name of the <see cref="$PropertyName$"/> <see cref="DependencyProperty"/>. /// </summary> public const string $PropertyName$Name = "$PropertyName$"; /// <summary> /// $end$ /// </summary> public $Type$ $PropertyName$ { get { return ($Type$)GetValue($PropertyName$Property); } set { SetValue($PropertyName$Property, value); } } #endregion ]]></Code> </Snippet> </CodeSnippet> </CodeSnippets> 
+1
source

All Articles