C# - 将 TextBox 绑定到一个整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1497489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 18:07:55  来源:igfitidea点击:

C# - Binding TextBox to an integer

c#winformsdata-binding

提问by david.healed

How to bind a TextBox to an integer? For example, binding unit to textBox1.

如何将 TextBox 绑定到整数?例如,将单元绑定到 textBox1。

public partial class Form1 : Form
{
    int unit;

    public Form1()
    {
        InitializeComponent();


    }

    private void Form1_Load(object sender, EventArgs e)
    {
        textBox1.DataBindings.Add("Text", unit, "???");
    }

采纳答案by Marc Gravell

It would need to be a public property of an instance; in this case, the "this" would suffice:

它需要是实例的公共属性;在这种情况下,“这个”就足够了:

public int Unit {get;set;}
private void Form1_Load(object sender, EventArgs e)
{
    textBox1.DataBindings.Add("Text", this, "Unit");
}

For two-way notification, you'll need either UnitChangedor INotifyPropertyChanged:

对于双向通知,您需要UnitChangedINotifyPropertyChanged

private int unit;
public event EventHandler UnitChanged; // or via the "Events" list
public int Unit {
    get {return unit;}
    set {
        if(value!=unit) {
            unit = value;
            EventHandler handler = UnitChanged;
            if(handler!=null) handler(this,EventArgs.Empty);
        }
    }
}

If you don't want it on the public API, you could wrap it in a hidden type somewhere:

如果您不想在公共 API 上使用它,您可以将它包装在某个隐藏类型中:

class UnitWrapper {
    public int Unit {get;set;}
}
private UnitWrapper unit = new UnitWrapper();
private void Form1_Load(object sender, EventArgs e)
{
    textBox1.DataBindings.Add("Text", unit, "Unit");
}

For info, the "events list" stuff goes something like:

有关信息,“事件列表”内容类似于:

    private static readonly object UnitChangedKey = new object();
    public event EventHandler UnitChanged
    {
        add {Events.AddHandler(UnitChangedKey, value);}
        remove {Events.AddHandler(UnitChangedKey, value);}
    }
    ...
    EventHandler handler = (EventHandler)Events[UnitChangedKey];
    if (handler != null) handler(this, EventArgs.Empty);

回答by XXXXX

You can use a binding source (see comment). The simplest change is:

您可以使用绑定源(请参阅评论)。最简单的改变是:

public partial class Form1 : Form
{
    public int Unit { get; set; }
    BindingSource form1BindingSource;

    private void Form1_Load (...)
    {
        form1BindingSource.DataSource = this;
        textBox1.DataBindings.Add ("Text", form1BindingSource, "Unit");
    }
}

However, you'll gain some conceptual clarity if you separate out the data a bit:

但是,如果您将数据分开一点,您将获得一些概念上的清晰度:

public partial class Form1 : Form
{
    class MyData {
        public int Unit { get; set; }
    }

    MyData form1Data;
    BindingSource form1BindingSource;

    private void Form1_Load (...)
    {
        form1BindingSource.DataSource = form1Data;
        textBox1.DataBindings.Add ("Text", form1BindingSource, "Unit");
    }
}

HTH. Note access modifiers omitted.

哈。注意省略了访问修饰符。

回答by Jim Lahman

One of the things I like to do is to create "presentation" layer for the form. It is in this layer that I declare the properties that are bound to the controls on the form. In this case, the control is a text box.

我喜欢做的一件事是为表单创建“表示”层。正是在这一层中,我声明了绑定到窗体上的控件的属性。在这种情况下,控件是一个文本框。

In this example I have a form with a textbox to display an IP Address

在这个例子中,我有一个带有文本框的表单来显示 IP 地址

enter image description here

在此处输入图片说明

We now create the binding source through the textbox properties. Select DataBindings->Text. Click the down arrow; select 'Add Project Data Source'.

我们现在通过文本框属性创建绑定源。选择数据绑定->文本。单击向下箭头;选择“添加项目数据源”。

enter image description here

在此处输入图片说明

This starts up that Data Source wizard. Select Object. Hit 'Next'.

这将启动该数据源向导。选择对象。点击“下一步”。

enter image description here

在此处输入图片说明

Now select the class that has the property that will be bounded to the text box. In this example, I chose PNetworkOptions. Select Finish to end the wizard. The BindingSource will not be created.

现在选择具有将绑定到文本框的属性的类。在这个例子中,我选择了PNetworkOptions。选择完成以结束向导。不会创建 BindingSource。

enter image description here

在此处输入图片说明

The next step is to select the actual property from the bound class. From DataBindings->Text, select the downarrow and select the property name that will be bound to the textbox.

下一步是从绑定类中选择实际属性。从 DataBindings->Text,选择向下箭头并选择将绑定到文本框的属性名称。

enter image description here

在此处输入图片说明

In the class that has your property, INotifyPropertyChanged must implemented for 2-way communication for IP Address field

在具有您的属性的类中,必须为 IP 地址字段的 2 路通信实现 INotifyPropertyChanged

public class PNetworkOptions : IBaseInterface, INotifyPropertyChanged
{
    private string _IPAddress;


    private void NotifyPropertyChanged(String info)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(info));
        }
    }

    public string IPAddress
    {
        get { return _IPAddress; }
        set
        {
            if (value != null && value != _IPAddress)
            {
                _IPAddress = value;
                NotifyPropertyChanged("IPAddress");
            }
        }
    }
}

In the form constructor, we have to specifically define the binding

在表单构造函数中,我们要专门定义绑定

Binding IPAddressbinding = mskTxtIPAddress.DataBindings.Add("Text", _NetOptions, "IPAddress",true,DataSourceUpdateMode.OnPropertyChanged);