wpf 我如何在属性更改时 RaisePropertyChanged?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15246108/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can i RaisePropertyChanged on property change?
提问by A.T.
Here I added a model to my viewmodel...
在这里,我向我的视图模型添加了一个模型...
public dal.UserAccount User {
get
{
return _user;
}
set
{
_user = value;
RaisePropertyChanged(String.Empty);
}
}
I handle property change event...
我处理属性更改事件...
public event PropertyChangedEventHandler PropertyChanged;
private void RaisePropertyChanged(string propertyName)
{
if (this.PropertyChanged != null)
this.PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
This is the binding i use.
这是我使用的绑定。
<TextBox Text="{Binding User.firstname, Mode=TwoWay, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}" />
Problem is propertychange event is not trigger on updating view ? Can anybody tell me what i am doing wrong...
问题是更新视图时不会触发 propertychange 事件吗?谁能告诉我我做错了什么...
回答by Blachshma
PropertyChanged is used to notify the UI that something has been changed in the Model.
Since you're changing an inner propertyof the Userobject - the User
property itself is not changed and therefore the PropertyChanged event isn't raised.
PropertyChanged 用于通知 UI 模型中的某些内容已更改。既然你改变一个内财产的的用户对象-的User
属性本身没有改变,因此PropertyChanged事件不会引发。
Second - your Model should implement the INotifyPropertyChangedinterface. - In other words make sure UserAccount
implements INotifyPropertyChanged, otherwise changing the firstname
will not affect the view either.
其次 - 您的模型应该实现INotifyPropertyChanged接口。- 换句话说,确保UserAccount
实现 INotifyPropertyChanged,否则更改firstname
也不会影响视图。
Another thing:
另一件事:
The parameter RaisePropertyChanged should receive is the Nameof the property that has changed. So in your case:
RaisePropertyChanged 应接收的参数是已更改的属性的名称。所以在你的情况下:
Change:RaisePropertyChanged(String.Empty);
改变:RaisePropertyChanged(String.Empty);
ToRaisePropertyChanged("User");
到RaisePropertyChanged("User");
From MSDN:
从MSDN:
The PropertyChanged event can indicate all properties on the object have changed by using either null or String.Empty as the property name in the PropertyChangedEventArgs.
通过使用 null 或 String.Empty 作为 PropertyChangedEventArgs 中的属性名称,PropertyChanged 事件可以指示对象上的所有属性都已更改。
(No need to refresh all the Properties in this case)
(在这种情况下无需刷新所有属性)
You can read more on the concept of PropertyChangedhere
回答by Mike de Klerk
You can invoke a property changed event from another class. Not particularly useful if you have all the sources. For closed source it might be. Though I consider it experimental and not production ready.
您可以从另一个类调用属性更改事件。如果您拥有所有来源,则不是特别有用。对于闭源,它可能是。虽然我认为它是实验性的,还没有准备好生产。
See this console copy paste example:
请参阅此控制台复制粘贴示例:
using System;
using System.ComponentModel;
using System.Runtime.InteropServices;
namespace ConsoleApp1
{
public class Program
{
static void Main(string[] args)
{
var a = new A();
a.PropertyChanged += A_PropertyChanged;
var excpl = new Excpl();
excpl.Victim = a;
excpl.Ghost.Do();
}
private static void A_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
Console.WriteLine("Event triggered");
}
}
[StructLayout(LayoutKind.Explicit)]
public struct Excpl
{
[FieldOffset(0)]
public A Victim;
[FieldOffset(0)]
public C Ghost;
}
public class A : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
}
public class C : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public void Do()
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(""));
}
}
}