问题描述
|
我有一个带有10个文本框的表单(CustomerInfoForm)。每个
TextBoxes
的默认Text
属性在设计时定义。子类“ 2”包含用于保存在表单中输入的数据的属性。包含数据的子类将被序列化为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\";
(我注意到在数据绑定之后才设置Text
属性)在IDE生成的代码中。
当我运行项目时,窗体将以默认值显示在ѭ1中。但是,当按ѭ7序列化MyForm.CustomerInfo
子类中的属性时,它们全为空。由于这些值仅会从表单中更改,因此我希望不必实现接口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; }
}
}
}
编辑-对于可能在此问题上发生的其他问题
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\",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; }
}
}
}
解决方法
首先,您需要决定是使用数据绑定还是直接操作“ 0”属性。这两种方法不应混在一起。
如果要使用数据绑定,则代码中缺少一行:
public Form1()
{
InitializeComponent();
customerInfoBindingSource.DataSource = ci; // This is the missing line
}
您需要让您的customerInfoBindingSource
了解数据源。
如果添加此行,则在设计时分配的“ 0”将被绑定数据源中的文本覆盖。如果要使用绑定,则应使用数据源进行操作,而不要直接设置“ 0”字段。像这样:
public Form1()
{
InitializeComponent();
ci.CustomerName = \"TestCustomerName\";
customerInfoBindingSource.DataSource = ci;
}