属性换的不会执行
我正在尝试以Windows表单实现MVP,我将所有控件都绑定到绑定源的相应属性,并且当用户更改某些内容并从控件中丢失焦点时,我希望将属性换的事件立即执行。 目前,只有当我输入主持人 - >例如view.username =“ sosings”;但是我不想要这个。
public string Username
{
get { return txtUsername.Text; }
set {
var oldValue = txtUsername.Text;
txtUsername.Text = value;
NotifyPropertyChanged("Name", oldValue, value);
}
}
public class PropertyChangedExtendedEventArgs : PropertyChangedEventArgs
{
public virtual object OldValue { get; private set; }
public virtual object NewValue { get; private set; }
public PropertyChangedExtendedEventArgs(string propertyName, object oldValue,
object newValue)
: base(propertyName)
{
OldValue = oldValue;
NewValue = newValue;
}
}
public partial class MasterForm : Form, INotifyPropertyChanged
{
public MasterForm()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName, object oldvalue, object newvalue)
{
PropertyChanged?.Invoke(this, new PropertyChangedExtendedEventArgs(propertyName, oldvalue, newvalue));
}
private void MasterForm_Load(object sender, EventArgs e)
{
}
}
I am trying to implement MVP in windows forms, i have all controls binded to a corresponding property of a binding source, and i want, when the user change something and lost focus from the control, the propertychanged event to get executed immediately.
For now it is only get executed if i type in presenter -> e.g. view.Username = "something"; but i dont want this.
public string Username
{
get { return txtUsername.Text; }
set {
var oldValue = txtUsername.Text;
txtUsername.Text = value;
NotifyPropertyChanged("Name", oldValue, value);
}
}
public class PropertyChangedExtendedEventArgs : PropertyChangedEventArgs
{
public virtual object OldValue { get; private set; }
public virtual object NewValue { get; private set; }
public PropertyChangedExtendedEventArgs(string propertyName, object oldValue,
object newValue)
: base(propertyName)
{
OldValue = oldValue;
NewValue = newValue;
}
}
public partial class MasterForm : Form, INotifyPropertyChanged
{
public MasterForm()
{
InitializeComponent();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName, object oldvalue, object newvalue)
{
PropertyChanged?.Invoke(this, new PropertyChangedExtendedEventArgs(propertyName, oldvalue, newvalue));
}
private void MasterForm_Load(object sender, EventArgs e)
{
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在问当用户更改某些内容并因控件而失去焦点时,属性换事的事件将立即执行。
此代码显示了如何捕获UI控件的
LostFocus
事件以启动所需的事件。You're asking When the user change something and lost focus from the control, the propertychanged event to get executed immediately.
This code shows how to capture the
LostFocus
event of your UI control to fire the event you want.