【问题标题】:Two way databinding in winforms, Inotifypropertychanged implemented in base classwinforms中的两种方式数据绑定,Inotifypropertychanged在基类中实现
【发布时间】:2013-10-19 15:20:17
【问题描述】:

我使用 Winforms 数据绑定,并且我有派生类,其中基类实现 IPropertychanged:

    public event PropertyChangedEventHandler PropertyChanged;

    private void RaisePropertyChanged(string propertyName) {
        var handler = this.PropertyChanged;
        if (handler != null) {
            handler(this, new PropertyChangedEventArgs(propertyName));
        }
    }

每个属性设置器调用:

    protected void SetField<T>(ref T field, T value, string propertyName) {
        if (!EqualityComparer<T>.Default.Equals(field, value)) {
            field = value;
            IsDirty = true;
            this.RaisePropertyChanged(propertyName);
        }
    }

一个典型的 Propertysetter:

    public String LocalizationItemId {
        get {
            return _localizationItemId;
        }
        set {
             SetField(ref _localizationItemId, value, "LocalizationItemId");
        }
    }

属性绑定到文本框的方式

        private DerivedEntity derivedEntity
        TextBoxDerivedEntity.DataBindings.Add("Text", derivedEntity, "Probenname");

如果我以编程方式将文本分配给文本框,则文本框不会显示它。但我可以手动编辑文本框。

【问题讨论】:

    标签: c# winforms data-binding


    【解决方案1】:

    我知道现在回答为时已晚,但是这个问题可以解决,如果您在绑定应该更改值时设置事件,如果您在属性值更改事件上设置它,您的问题将得到解决。你可以通过这种方式做到这一点

    textBox.DataBindings.Add("textBoxProperty", entity, "entityProperty", true, DataSourceUpdateMode.OnPropertyChanged);
    

    【讨论】:

    • 你能发布一个用于文本框双向数据绑定的小工作代码吗?只看你的小代码无法想象如何实现它?
    • 感谢您的回复。我如何绑定其他控件,例如复选框、单选按钮和下拉列表等。我想以某种方式绑定这些控件,因为当数据源中的数据发生更改然后更改时应该是反映在控制层面。你能帮我提供一些示例代码吗,比如如何将这些控件与数据绑定checkboxes, radio button and dropdown etc
    • 您可以绑定复选框和单选控件的值属性。像这样 checkBox.DataBindings.Add("Value", entity, "entityProperty", true, DataSourceUpdateMode.OnPropertyChanged);
    • 如果你有数据绑定的combobox,你可以像这样绑定Combobox的SelectedItem checkBox.DataBindings.Add("SelectedItem", entity, "entityProperty", true, DataSourceUpdateMode.OnPropertyChanged)。
    【解决方案2】:

    在 TextBox Validated 事件上更新绑定源。当用户编辑 TextBox 然后将焦点更改为其他控件时,将调用 TextBox 验证事件。 由于您正在以编程方式更改 TextBox 文本,因此 TextBox 不知道文本已更改,因此不会调用验证并且不会更新绑定,因此您需要手动更新绑定。

    初始化绑定:

    var entity;
    textBox.DataBindings.Add("textBoxProperty", entity, "entityProperty");
    

    更改 TextBox.Text:

    textBox.Text = "SOME_VALUE";
    

    手动更新绑定:

    textBox.DataBindings["textBoxProperty"].WriteValue();
    

    Binding.WriteValue() 从控件中读取值并相应地更新实体。 您可以在 MSDN 阅读有关 WriteValue 的信息。

    【讨论】:

      【解决方案3】:

      订阅者未初始化。即

      private DerivedEntity derivedEntity
      TextBoxDerivedEntity.DataBindings.Add("Text", derivedEntity, "Probenname");
      

      derivedEntity 为空。

      初始化它,你会没事的。

      【讨论】:

        【解决方案4】:

        我实现了“INotifyPropertyChanged”,但只有在新值与旧值不同时才引发 PropertyChanged 事件:

        public class ProfileModel : INotifyPropertyChanged
        {
            private Guid _iD;
            private string _name;
            public event PropertyChangedEventHandler PropertyChanged;
        
            public Guid ID
            {
                get => _iD;
                set
                {
                    if (_iD != value)
                    {
                        _iD = value;
                        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ID"));
                    }
                }
            }
        
            public string Name
            {
                get => _name;
                set
                {
                    if (_name != value)
                    {
                        _name = value;
                        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("Name"));
                    }
                }
            }
        }
        

        现在只需绑定到控件:

        txtProfileID.DataBindings.Clear();
        txtProfileID.DataBindings.Add("Text", boundProfile, "ID", true, DataSourceUpdateMode.OnPropertyChanged);
        

        【讨论】:

          猜你喜欢
          • 2017-11-18
          • 1970-01-01
          • 2020-07-07
          • 2020-05-08
          • 1970-01-01
          • 2016-02-27
          • 2011-11-17
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多