如何使用 MVVM 设计从 WPF 中的视图模型更新视图

发布于 2024-11-06 15:02:41 字数 112 浏览 0 评论 0 原文

我在第一个项目中使用带有 MVVM 设计的 WFP,在处理来自客户端的更新实体的命令后,我遇到更新视图的问题。此时,视图可以与视图模型对话,但视图模型无法与视图对话。 任何人都知道如何使其有效? 谢谢, 吉当

I am using the WFP with MVVM design for my first project and I am 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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦行七里 2024-11-13 15:02:41

您应该使 ViewModel 实现 INotifyPropertyChanged 并在 ViewModel 属性更改时触发 PropertyChanged 事件。假设 UI 绑定到 ViewModel 属性,这应该可以工作。

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.

吻安 2024-11-13 15:02:41

在典型的 MVVM 应用程序中,您使用绑定将视图连接到 ViewModel。当 ViewModel 引发由 INotifyPropertyChanged 接口定义的 PropertyChanged 事件时,绑定会自动更新。因此,您需要在 ViewModel 中实现该接口,并在属性值更改时引发 PropertyChanged 事件,视图将自动反映更改。

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.

围归者 2024-11-13 15:02:41

如果您是 MVVM 模式的新手,我推荐以下来自 MSDN 的优秀资源,其中涵盖了该模式以及如何在 WPF 和 Silverlight 应用程序中实现它:

  1. 实现 MVVM 模式
  2. 高级 MVVM 场景

根据您所说,听起来您可能想回顾一下数据绑定,以及如何进行利用 INotifyPropertyChangedINotifyCollectionChanged ICollectionView 接口,支持视图和视图模型之间的双向通信。

Silverlight 和 WPF 数据绑定支持多种数据绑定模式。通过单向数据绑定,UI 控件可以绑定到视图模型,以便它们在呈现显示时反映基础数据的值。当用户在 UI 中修改底层数据时,双向数据绑定也会自动更新底层数据。为了确保当视图模型中的数据发生更改时 UI 保持最新,它应该实现适当的更改通知接口。

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.

败给现实 2024-11-13 15:02:41

除了其他答案之外,我还建议您的 ViewModel 扩展 DependencyObject

有些人认为 DependencyObject 很重(如果您创建了数千个实例,那么它们也可能很重)并且对于新用户来说有点复杂(肯定在某些情况下确实如此)。然而,DependencyObjects 还有其他优点,例如自动支持属性更改通知和绑定评估速度。

这是我的 DP 片段(在 C:\Users[您的名字]\Documents\Visual Studio [2010, 2008]\Code Snippets\Visual C#\My Code Snippets 中另存为 DependencyProperty.snippet):

<?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>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文