绑定属性以在Winforms中控制

cub*_*ski 42 c# vb.net binding winforms

将属性绑定到控件的最佳方法是什么,以便在更改属性值时,控件的绑定属性随之更改.

所以,如果我有一个属性FirstName,我想绑定到文本框的txtFirstName文本值.因此,如果我更改FirstName为值"Stack",则属性txtFirstName.Text也会更改为值"Stack".

我知道这可能听起来是一个愚蠢的问题,但我会感谢你的帮助.

Ste*_*cya 49

您必须实现INotifyPropertyChanged并添加绑定到文本框.

我将提供C#代码段.希望能帮助到你

class Sample : INotifyPropertyChanged
{
    private string firstName;
    public string FirstName
    {
        get { return firstName; }
        set
        {
            firstName = value;
            InvokePropertyChanged(new PropertyChangedEventArgs("FirstName"));
        }
    }

    #region Implementation of INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    public void InvokePropertyChanged(PropertyChangedEventArgs e)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, e);
    }

    #endregion
}
Run Code Online (Sandbox Code Playgroud)

用法:

 Sample sourceObject = new Sample();
 textbox.DataBindings.Add("Text",sourceObject,"FirstName");
 sourceObject.FirstName = "Stack"; 
Run Code Online (Sandbox Code Playgroud)

  • 要将控件中的更改绑定到属性,请使用此`textbox.DataBindings.Add("Text",sourceObject,"FirstName",false,DataSourceUpdateMode.OnPropertyChanged); (4认同)
  • 当`textBox.Text`改变时,这将更新`sourceObject`.当`sourceObject.FirstName`改变时(这是原始问题),你还需要订阅`PropertyChanged`事件来更新文本框内容. (3认同)
  • @Stecya这不完全正确.鉴于上面的例子,它将在我更改`sourceObject.FirstName`时起作用,当我更改UI中文本框中的文本并离开文本框时它将起作用.但是,当我通过执行`textbox.Text ="Anything"更新代码隐藏中的文本框文本时,它不会更新`sourceObject`中的属性.所以我认为绑定更新是由.NET在任何控件的内部事件事件上触发的,不是吗?当我错了,请纠正我.对我来说,这种情况还不够. (3认同)
  • 真好.虽然我建议使用方法自动获取属性名称以方便:`protected void OnPropertyChanged(){if(PropertyChanged!= null)PropertyChanged(this,new PropertyChangedEventArgs(new System.Diagnostics.StackFrame(1).GetMethod().Name .Replace("set_",""))); }` (3认同)
  • @David - 这有两种方式.刚刚创建了应用程序并对其进 (2认同)

Dmi*_*mov 5

一个简化版本接受的答案的,不需要你在每一个属性setter喜欢手动键入属性的名称OnPropertyChanged("some-property-name")。相反,您只是OnPropertyChanged()不带参数调用:

您至少需要.NET 4.5。 CallerMemberNameSystem.Runtime.CompilerServices名称空间中

public class Sample : INotifyPropertyChanged
{
    private string _propString;
    private int _propInt;


    //======================================
    // Actual implementation
    //======================================
    public event PropertyChangedEventHandler PropertyChanged;

    [NotifyPropertyChangedInvocator]
    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        var handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    //======================================
    // END: actual implementation
    //======================================


    public string PropString
    {
        get { return _propString; }
        set
        {
            // do not trigger change event if values are the same
            if (Equals(value, _propString)) return;
            _propString = value;

            //===================
            // Usage in the Source
            //===================
            OnPropertyChanged();

        }
    }

    public int PropInt
    {
        get { return _propInt; }
        set
        {
            // do not allow negative numbers, but always trigger a change event
            _propInt = value < 0 ? 0 : value;
            OnPropertyChanged();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

用法保持不变:

var source = new Sample();
textbox.DataBindings.Add("Text", source, "PropString");
source.PropString = "Some new string";
Run Code Online (Sandbox Code Playgroud)

希望这对某人有帮助。