简体   繁体   English

如何将Winform文本框绑定到类属性?

[英]How can I bind a Winform textbox to a class property?

I have read a number of questions on Stack Overflow and elsewhere that describe tying a textbox to a class but I cannot seem to even get the basics working without receiving an error from VS when compiling. 我已经在Stack Overflow和其他地方阅读了许多问题,这些问题描述了如何将文本框绑定到类,但是在编译时如果没有收到VS的错误,我似乎甚至无法使基础工作。

(1) What I want to accomplish is to display the text of a property from a class. (1)我要完成的是显示一个类的属性的文本。

(2) When the user modifies that text, I want the property to automatically update. (2)当用户修改该文本时,我希望该属性自动更新。

Unfortunately I cannot even get past (1) yet. 不幸的是,我什至无法超越(1)。

The class: 班级:

class BookProperties : INotifyPropertyChanged
{

    private string _bookTitle;
    public string bookTitle { get { return _bookTitle; } set { SetField(ref _bookTitle, value, "bookTitle"); } }

    #region handle property changes

    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string propertyName)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }
    protected bool SetField<T>(ref T field, T value, string propertyName)
    {
        if (EqualityComparer<T>.Default.Equals(field, value)) return false;
        field = value;
        OnPropertyChanged(propertyName);
        return true;
    }

    #endregion
}

The class initializer: 类的初始化器:

BindingList<BookProperties> bookProperty = new BindingList<BookProperties>();

The connection to the textbox: 与文本框的连接:

    textBox1.DataBindings.Clear();
    textBox1.DataBindings.Add("Text", bookProperty, "bookProperty.bookTitle");

I have also tried this: 我也尝试过这个:

    textBox1.DataBindings.Clear();
    textBox1.DataBindings.Add("Text", bookProperty, "bookProperty[0].bookTitle");

Visual Studio throws the following error: Visual Studio引发以下错误:

Child list for field bookProperty cannot be created. 无法创建fieldbookProperty的子级列表。

START EDIT: Trying this code, I remove the additional element from the third parameter as a few have suggested. 开始编辑:尝试这段代码,我建议从第三个参数中删除其他元素。

bookProperty.Add(new BookProperties(){bookTitle="C#"});
textBox1.DataBindings.Add("Text", bookProperty[0], "bookTitle");

Now, I receive this error. 现在,我收到此错误。 I had received it before and searched for a resolution but think it might be too generic for me to figure out what exactly I am doing wrong. 我以前收到过它,正在寻找解决方案,但认为对我来说太笼统了,无法弄清楚我到底在做什么错。

An unhandled exception of type 'System.ArgumentException' occurred in System.Windows.Forms.dll System.Windows.Forms.dll中发生了类型为'System.ArgumentException'的未处理异常

Additional information: This causes two bindings in the collection to bind to the same property. 附加信息:这将导致集合中的两个绑定绑定到同一属性。

END EDIT 结束编辑

I am starting to think there is something fundamentally wrong with my approach since I have seen similar code that people say works so I am hopeful someone can tell me what I am doing wrong. 我开始认为我的方法存在根本上的错误,因为我已经看到人们说的类似代码有效,所以我希望有人可以告诉我我做错了什么。 Please note this is a Windows Form issue, not XAML. 请注意,这是Windows窗体问题,而不是XAML。 Thanks. 谢谢。

FIXED Sorry about the last edit. 已修正对不起,最后一次编辑。 It turns out I had previously attempted to manually link the textbox to a data source using the VS designer. 事实证明,我以前曾尝试使用VS设计器将文本框手动链接到数据源。 After I removed that data source, everything worked. 删除该数据源后,一切正常。 Thank you for the help! 感谢您的帮助!

Try this: 尝试这个:

bookProperty.Add(new BookProperties(){bookTitle="C#"});
textBox1.DataBindings.Add("Text", bookProperty[0], "bookTitle");

Second argument is source that should be shown, third parameter is source class property. 第二个参数是应该显示的源,第三个参数是源类的属性。 Also make sure there is items in bookProperty list. 还要确保bookProperty列表中有项目。

Hope helps. 希望会有所帮助。

确保您正在捕获更改:

textBox1.DataBindings.Add("Text", bookProperty[0], "bookTitle", true, DataSourceUpdateMode.OnPropertyChanged);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM