如何在 ViewModel 中订阅 PropertyChanged 事件?

发布于 2024-12-10 23:49:13 字数 381 浏览 0 评论 0原文

我将核心功能封装在 ViewModelBase 中

现在我想查看 ViewModelBase 何时引发 PropertyChanged 事件并对其采取行动。例如,当 ViewModelBase 上的一个属性发生更改时 - 我想更改 ViewModel 上的属性,

如何实现这一目标?

public class MaintainGroupViewModel : BaseViewModel<MEMGroup>
    {


public abstract class BaseViewModel<T> : NotificationObject, INavigationAware
        where T : Entity
    {

I have core functionality encapsulated in ViewModelBase

Now I want to see when PropertyChanged event was raised by ViewModelBase and act on it. For example, when one property was changed on ViewModelBase - I want to change property on my ViewModel

How do I achieve this?

public class MaintainGroupViewModel : BaseViewModel<MEMGroup>
    {


public abstract class BaseViewModel<T> : NotificationObject, INavigationAware
        where T : Entity
    {

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

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

发布评论

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

评论(3

禾厶谷欠 2024-12-17 23:49:13

通常我在类构造函数中使用注册到 PropertyChanged 事件

public MyViewModel()
{
    this.PropertyChanged += MyViewModel_PropertyChanged;
}

,我的 PropertyChanged 事件处理程序如下所示:

void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "SomeProperty":
            // Do something
            break;
    }
}

Usually I use register to the PropertyChanged event in the class Constructor

public MyViewModel()
{
    this.PropertyChanged += MyViewModel_PropertyChanged;
}

and my PropertyChanged event handler looks like this:

void MyViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
    switch (e.PropertyName)
    {
        case "SomeProperty":
            // Do something
            break;
    }
}
不羁少年 2024-12-17 23:49:13

我担心您实际上正在对派生类中的属性进行“手动绑定”(不好)到基类上的值(也不好)。使用继承的全部意义在于派生类可以访问基类中的内容。使用 protected 修饰符来指示事物只能由派生类访问。

我建议这个(可能)更正确的方法:

基类:

protected virtual void OnMyValueChanged() { }

派生类:

protected override void OnMyValueChanged() { /* respond here */ }

真的,订阅您正在编写的类的基类中的事件似乎令人难以置信的倒退 - 如果您使用继承而不是组合,那有什么意义呢?你要让自己平静下来吗?当事情发生时,你实际上是在要求一个对象告诉自己。为此,您应该使用方法调用。

就“当 ViewModelBase 上的一个属性发生更改时 - 我想更改我的 ViewModel 上的属性”而言,...它们是同一个对象!

I am concerned that you're effectively doing a 'manual binding' (bad) for a property in a derived class to a value on the base class (also bad). The whole point of using inheritance is that the derived class can access things in the base class. Use a protected modifier to indicate things should only be accessible to derived classes.

I would suggest this (potentially) more correct method:

Base class:

protected virtual void OnMyValueChanged() { }

Derived class:

protected override void OnMyValueChanged() { /* respond here */ }

Really, subscribing to an event in the base class of the very class you're writing just seems incredibly backwards - what's the point of using inheritance over composition if you're going to compose yourself around yourself? You're literally asking an object to tell itself when something happens. A method call is what you should use for that.

In terms of "when one property was changed on ViewModelBase - I want to change property on my ViewModel", ... they are the same object!

幸福还没到 2024-12-17 23:49:13

订阅属性更改的直接方法是使用 INotifyPropertyChanged 如果您的 BaseViewModel 实现了它:

PropertyChanged += (obj, args) =>
   { System.Console.WriteLine("Property " + args.PropertyName + " changed"); }

如果没有,那么它必须是 DependencyObject,并且您的属性必须是DependencyProperties(这可能是一种更复杂的方式)。

本文介绍如何订阅 DependencyProperty 更改。

The direct way to subscribe to property changes is using INotifyPropertyChanged if your BaseViewModel implements it:

PropertyChanged += (obj, args) =>
   { System.Console.WriteLine("Property " + args.PropertyName + " changed"); }

If it doesn't, then it has to be a DependencyObject, and your properties have to be DependencyProperties (which is probably a more complicated way).

This article describes how to subscribe for DependencyProperty changes.

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