开发者

How to update the view from a viewmodel in WPF using MVVM design

I am using the WFP with MVVM design for my first project and I 开发者_如何学Cam having the problem to update the view after I process a command from client to update the entity. At this time, the view can talks to the viewmodel but the viewmodel could not talk back to view. Anyone has any idea how to make this works? Thanks, Jdang


You should make the ViewModel implement INotifyPropertyChanged and fire the PropertyChanged event when the ViewModel properties change. Assuming that the UI is bound to the ViewModel properties this should work.


If you are new to the MVVM Pattern, I recommend the following as excellent resources from MSDN that cover both the pattern and how to implement it in WPF and Silverlight applications:

  1. Implementing the MVVM Pattern
  2. Advanced MVVM Scenarios

Based on what you have said, it sounds like you might want to review data binding, and how you can leverage the INotifyPropertyChanged, INotifyCollectionChanged, and ICollectionView interfaces to enable two-way communication between your views and view models.

Silverlight and WPF data binding supports multiple data binding modes. With one-way data binding, UI controls can be bound to a view model so that they reflect the value of the underlying data when the display is rendered. Two-way data binding will also automatically update the underlying data when the user modifies it in the UI. To ensure that the UI is kept up to date when the data changes in the view model, it should implement the appropriate change notification interface.


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. So you need to implement that interface in the ViewModel and raise the PropertyChanged event when the value of a property is changed, and the view will reflect the change automatically.


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

Some people believe DependencyObjects are heavy weight (and they can be, if you create thousands of instances of them) and a bit complex for new users (most definitely there are situations where this is true). There are, however, other advantages to DependencyObjects, such as automatic support for property change notification and speed of binding evaluation.

Here's my DP snippet (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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜