c# - INotifyPropertyChanged - 更改 PropertyName 没有效果

标签 c# winforms inotifypropertychanged

我正在尝试将我的 Winforms UI 绑定(bind)到我的 ViewModel。我能够在 UI 更改时成功更新我的 ViewModel,反之亦然。但是,我似乎无法理解 PropertyChangedEventHandler 中使用的“PropertyName”的用途是什么,因为无论我放在那里什么,它都会一直有效。我不知道我是否已经把事情搞混了,因为我已经阅读了很多关于架构模式的文章(MVP、MVC、MVVM 和 MVP-VM(这是我现在尝试做的))。

相关代码部分如下:

View 模型

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 上绑定(bind)):

// 
// 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 上看到的那样,我在 setter 中省略了每个 PropertyName 开头的所有“A”,但应用程序仍在运行。 很抱歉粘贴了很长的代码。我似乎找不到比向您展示实现更好的解释了

最佳答案

INotifyPropertyChanged 不是数据绑定(bind)所必需的,但它支持双向数据绑定(bind)。
事实上,如文档中所述:INotifyPropertyChanged 接口(interface)用于通知客户端(通常是绑定(bind)客户端)属性值已更改。

在简单(单向)数据绑定(bind)中,当您更改控件的绑定(bind)属性时,值会推送到对象的绑定(bind)属性中,它不需要 INotifyPropertyChanges .

但没有INotifyPropertyChanged ,如果您使用代码更改对象的绑定(bind)属性的值,则新值不会推送到控件的绑定(bind)属性中。


在 PropertyChanged 事件中有错误的属性名称为什么我仍然有双向数据绑定(bind)?

其实是因为用了BindingSource作为数据绑定(bind)的来源,如 Fabio 所述在评论中。

使用 BindingSource 时作为数据绑定(bind)的数据源,您的对象足以实现 INotifyPropertyChanged并提高 PropertyChaned事件(即使属性名称为空或错误),然后是 BindingSource (实际上它的内部 BindingList<T> ) subscribes对于 PropertyChaned事件和时间received如果您没有传递正确的属性名称,或者如果您传递了空属性名称,它将检查的事件将调用 ResetBindings() 结果导致控件绑定(bind)到 BindingSource重新读取列表中的所有项目并刷新它们的显示值。 更正 PropertyChanged 中的名称导致双向数据绑定(bind)的正常行为,也导致提高 ListChanged e.PropertyDescriptor 中具有正确属性的事件.

关于c# - INotifyPropertyChanged - 更改 PropertyName 没有效果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34253278/

相关文章:

c# - 面板滚动后,面板内的 DataGridView 跳转到列表的开头

c# - Linux 上的 RichTextBox 编码 - Mono

c# - WPF 为获取属性通知 PropertyChanged

vb.net - 如何将属性数据绑定(bind)到 Wpf 中的文本框

c# - C# 中的变体规则

c# webclient 从ssl上传和下载数据

c# - 如何在用户控件中的两个控件之间绘制直线?

c# - 当 Item 改变时通知 ObservableCollection

c# - SvgViewbox 在鼠标悬停时不显示工具提示

c# - 将 'New' 数组从 VB.Net 转换为 C#