【发布时间】:2015-12-13 16:19:10
【问题描述】:
我正在尝试将我的 Winforms UI 绑定到我的 ViewModel。我能够在 UI 更改时成功更新我的 ViewModel,反之亦然。但是,我似乎无法理解 PropertyChangedEventHandler 中使用的“PropertyName”的用途是什么,因为无论我放在那里,它都将始终有效。我不知道我是否已经把事情搞混了,因为我已经阅读了很多关于架构模式的文章(MVP、MVC、MVVM 和 MVP-VM(这是我现在尝试做的))。
这是相关代码的一部分:
视图模型
public class AdditionViewModel:INotifyPropertyChanged
{
private string augend;
public string Augend
{
get { return augend; }
set {
if(augend != value)
{
augend = value;
OnPropertyChanged(new PropertyChangedEventArgs("ugend"));
}
}
}
private string addend;
public string Addend
{
get { return addend; }
set {
if (addend != value)
{
addend = value;
OnPropertyChanged(new PropertyChangedEventArgs("ddend"));
}
}
}
private string answer;
public string Answer
{
get { return answer; }
set {
if(answer != value)
{
answer = value;
OnPropertyChanged(new PropertyChangedEventArgs("nswer"));
}
}
}
public AdditionClass additionClass;
public AdditionViewModel(AdditionClass _additionClass)
{
additionClass = _additionClass;
}
public void Add()
{
//Some verifications first before inserting the value to the model class
this.Augend = "1";//Testing for from code to UI binding
additionClass.Augend = Double.Parse(Augend);
additionClass.Addend = Double.Parse(Addend);
//Somewhere here should implement the compute but since addition is a very simple task, no methods were called;
Answer = additionClass.Answer.ToString();
}
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged(PropertyChangedEventArgs e)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
MessageBox.Show(e.PropertyName);
handler(this, new PropertyChangedEventArgs(e.PropertyName));
}
}
}
表格:
private void Form1_Load(object sender, EventArgs e)
{
additionPresenter = new Presenter.AdditionPresenter(new ViewModel.AdditionViewModel(new Model.AdditionClass()));
additionViewModelBindingSource.DataSource = additionPresenter.additionViewModel;
}
private void button1_Click(object sender, EventArgs e)
{
additionPresenter.AddButtonClick();
}
演讲者:
public AdditionPresenter(AdditionViewModel _additionViewModel)
{
additionViewModel = _additionViewModel;
}
public void AddButtonClick()
{
additionViewModel.Add();
}
Designer 中自动生成的代码之一(在 UI 上绑定):
//
// textBox1
//
this.textBox1.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.additionViewModelBindingSource, "Addend", true));
this.textBox1.Location = new System.Drawing.Point(24, 41);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 20);
this.textBox1.TabIndex = 0;
从 ViewModel 上可以看出,我在设置器中省略了每个 PropertyName 开头的所有“A”,但应用程序仍在工作。 对不起,长代码粘贴。我似乎找不到比仅仅向您展示实现更好的解释
【问题讨论】:
-
因为您使用
BindingSource作为表单中的“ViewModel”对象。将数据绑定更改为不使用BindingSource=>textBox1.DataBinding.Add("Text", additionViewModel, "Addend", true)然后NotifyPropertyChanged事件将产生影响 - 或不会对您的情况产生影响 -
那么是否意味着通过使用
BindingSource作为控件的BindingSource,它已经启用了控件与对象的双向绑定?正如你所说,通过使用 ViewModel 作为BindingSource,会使PropertyName很重要并影响绑定? -
我已经实际测试了您所说的内容,并且通过使用 ViewModel 进行数据绑定而不是
BindingSource,现在要求PropertyName与 @987654335 中的相同@在数据绑定。既然两者都有效,那么哪个更受欢迎?或者我应该为我的新问题发布另一个帖子。 -
我想如果你创建另一个线程你会得到更多的答案,我希望。在我自己的意见中,我使用
BindingSource在 UI 具有网格和当前在同一视图/表单上选择的项目信息的情况下 - 我使用BindingSource的Current属性。但在其他情况下,我更喜欢实现INotifyPropertyChanged接口的 ViewModel 实例。这让我“感觉”我可以控制。我不确定,但经过测试,BindingSource似乎会在每次更新某些属性后循环所有 ViewModel 属性。 -
@Fabio 我编辑了帖子并提供了有关 PropertyChanged 事件中的属性名称错误为什么我仍然有双向数据绑定的详细说明? 您可以阅读使用我在答案中提供的链接的源代码。希望答案对您有所帮助:)
标签: c# winforms inotifypropertychanged