检测OnPropertyChanged何时发生(Detect when a OnPropertyChanged occurs)

编程入门 行业动态 更新时间:2024-10-24 04:44:02
检测OnPropertyChanged何时发生(Detect when a OnPropertyChanged occurs)

这是我需要的简化示例: 我有A类,有一个属性Name。 Name属性是异步更改的,无法知道修改何时发生。 为了在视图中显示它的更新值,我在其中连接了propertychanged事件并将其绑定到{Binding A.Name} 。 在VM中它工作正常。 但在我的情况下,有很多自定义属性不应该在类A中。我想在类A中引发propertychanged时, AViewModel的Name属性应该得到通知并提升AViewModel

有没有办法这样做?

C# :

public class A : BaseViewModel { string name; public string Name { get { return name; } set { Set(()=> Name, ref name, value); } } } public class AViewModel : BaseViewModel { A a; public A A { get { return a; } set { Set(()=> A, ref a, value); } } public string Name { get { return A.Name; } set { Set(()=> Name, ref A.Name, value); } } }

XAML:

<TextBox Text="{Binding Name}" />

Here is a simplified example of what i need: I have class A that have a property Name. the Name property is changed asynchronously and there is no way to know when the modification occurs. In order to show the updated value of it in the view, I wired a propertychanged event in it and bind it with {Binding A.Name}. In the VM it works fine. But in my case, there is a lot of custom properties that shouldn't be in the class A. I'm thinking once propertychanged is raised in class A, the Name property in AViewModel should get notified and raise the OnPropertyChanged too

is there any way to do so ?

C# :

public class A : BaseViewModel { string name; public string Name { get { return name; } set { Set(()=> Name, ref name, value); } } } public class AViewModel : BaseViewModel { A a; public A A { get { return a; } set { Set(()=> A, ref a, value); } } public string Name { get { return A.Name; } set { Set(()=> Name, ref A.Name, value); } } }

XAML :

<TextBox Text="{Binding Name}" />

最满意答案

例如,A类必须具有经典的C#事件,因此AViewModel可以订阅它。

The class A must have a classic C# event, for example, so the AViewModel can subscribe it.

更多推荐

本文发布于:2023-08-05 18:35:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1435787.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:发生   OnPropertyChanged   occurs   Detect

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!