将数据绑定到WinForm

6
我有一个名为CustomerInfoForm的表单,其中包含10个TextBox。每个TextBox的默认Text属性在设计时已定义。一个名为CustomerInfoForm.CustomerInfo的子类包含了用于存储表单中输入数据的属性。包含数据的子类将被序列化为XML。
在自动生成的表单代码中,每个文本框都有一行代码将数据源绑定到文本框。
this.customerInfoBindingSource = new System.Windows.Forms.BindingSource(this.components);

每个文本框的代码都是由C# IDE自动生成的:

this.txtCustomer.DataBindings.Add(new System.Windows.Forms.Binding("Text", this.customerInfoForm_CustomerInfoBindingSource, "CustomerName", true));
this.txtCustomer.Location = new System.Drawing.Point(60, 23);
this.txtCustomer.Name = "txtCustomer";
this.txtCustomer.Size = new System.Drawing.Size(257, 20);
this.txtCustomer.TabIndex = 0;
this.txtCustomer.Text = "CustomerName";

我注意到在IDE生成的代码中,“Text”属性直到DataBinding之后才被设置。
当我运行该项目时,表单将显示TextBoxes中的默认值。但是,当按下“SaveButton”以序列化“MyForm.CustomerInfo”子类中的属性时,它们全部为null。由于这些值仅从表单更改,因此我希望不必实现“INotifyPropertyChanged”接口。
我是否遗漏了一些基本或简单的东西?
以下是包括数据序列化的表单代码。
using System;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;

namespace SimpleCustomerInfo
{
    // You must apply a DataContractAttribute or SerializableAttribute
    // to a class to have it serialized by the DataContractSerializer.

    public partial class CustomerInfoForm : Form
    {
        CustomerInfo ci = new CustomerInfo();

        public CustomerInfoForm()
        {
            InitializeComponent();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            DataContractSerializer serializer = new DataContractSerializer(typeof(CustomerInfo));
            FileStream writer = new FileStream(@"C:\Users\Me\temp\testme.xml", FileMode.Create);
            serializer.WriteObject(writer,ci);
            writer.Close();
        }

        [DataContract(Name = "Customer", Namespace = "net.ElectronicCanvas")]
        public class CustomerInfo
        {
            [DataMember]
            public string CustomerName { get; set; }
            [DataMember]
            public PhoneInfo PhonePrimary { get; set; }
            [DataMember]
            public PhoneInfo PhoneDays { get; set; }
            [DataMember]
            public PhoneInfo PhoneEvening { get; set; }
        }

        public class PhoneInfo
        {
            public string number { get; set; }
            public string type { get; set; }
            public bool textOk { get; set; }
        }
    }
}

编辑 -- 对于其他可能遇到这个问题的人

这里是关于IT技术的内容。
using System;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Runtime.Serialization;

namespace SimpleCustomerInfo
{


    public partial class CustomerInfoForm : Form
    {
        CustomerInfo ci;

        public CustomerInfoForm()
        {
            InitializeComponent();
            ci = new CustomerInfo();
            ci.CustomerName = "My Customer Name";
            ci.PhoneDays.number = "888-888-8888";
            customerInfoForm_CustomerInfoBindingSource.DataSource = ci;

        }

        private void btnSave_Click(object sender, EventArgs e)
        {

            DataContractSerializer serializer = new DataContractSerializer(typeof(CustomerInfo));
            FileStream writer = new FileStream(@"C:\Users\me\temp\testme.xml", FileMode.Create);
            serializer.WriteObject(writer,ci);
            writer.Close();
        }
        // You must apply a DataContractAttribute or SerializableAttribute
        // to a class to have it serialized by the DataContractSerializer.
        [DataContract(Name = "Customer", Namespace = "net.ElectronicCanvas")]
        public class CustomerInfo
        {
            [DataMember]
            public string CustomerName { get; set; }
            [DataMember]
            public PhoneInfo PhonePrimary { get; set; }
            [DataMember]
            public PhoneInfo PhoneDays { get; set; }
            [DataMember]
            public PhoneInfo PhoneEvening { get; set; }

            // Constructor is needed to instantiate the PhoneInfo classes
            // within the CustomerInfo class
            public CustomerInfo()
            {
                PhonePrimary = new PhoneInfo();
                PhoneDays = new PhoneInfo();
                PhoneEvening = new PhoneInfo();
            }
        }

        public class PhoneInfo
        {
            public string number { get; set; }
            public string type { get; set; }
            public bool textOk { get; set; }
        }
    }
}
1个回答

3

首先您需要决定是使用数据绑定还是直接操作Text属性。这两种方法不应混合使用。

如果您想使用数据绑定,则代码中缺少一行:

public Form1()
{
    InitializeComponent();
    customerInfoBindingSource.DataSource = ci;  // This is the missing line
}

你需要让你的customerInfoBindingSource知道数据源。
如果你添加了这行代码,那么在设计时分配的Text将被来自绑定数据源的文本覆盖。如果你想使用绑定,应该操作数据源而不是直接设置Text字段。像这样:
public Form1()
{
    InitializeComponent();

    ci.CustomerName = "TestCustomerName";
    customerInfoBindingSource.DataSource = ci;
}

感谢您的回复。我进入了Windows表单生成的代码,并发现包含“DataSource”的行应该是customerInfoForm_CustomerInfoBindingSource.DataSource = ci; - DarwinIcesurfer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接