Dar*_*fer 6 .net c# xml data-binding winforms
我有一个带有10个TextBox的表单(CustomerInfoForm).Text
每个默认属性TextBoxes
都是在设计时定义的.子类CustomerInfoForm.CustomerInfo
包含用于保存在表单中输入的数据的属性.包含数据的子类将序列化为XML.
在自动生成的表单代码中,每个文本框都有一行代码将数据源绑定到文本框
this.customerInfoBindingSource = new System.Windows.Forms.BindingSource(this.components);
Run Code Online (Sandbox Code Playgroud)
每个文本框由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";
Run Code Online (Sandbox Code Playgroud)
(我注意到Text
在IDE生成的代码中,直到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; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑 - 对于可能在这个问题上发生的其他人
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; }
}
}
}
Run Code Online (Sandbox Code Playgroud)
首先,您需要决定是使用数据绑定还是Text
直接操作属性。这两种方法不应混合在一起。
如果您想使用数据绑定,则代码中缺少一行:
public Form1()
{
InitializeComponent();
customerInfoBindingSource.DataSource = ci; // This is the missing line
}
Run Code Online (Sandbox Code Playgroud)
您需要让您customerInfoBindingSource
了解数据源。
如果添加此行,则Text
在设计时分配的 将会被绑定数据源中的文本覆盖。如果您想使用绑定,您应该使用数据源进行操作,而不是Text
直接设置字段。像这样:
public Form1()
{
InitializeComponent();
ci.CustomerName = "TestCustomerName";
customerInfoBindingSource.DataSource = ci;
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1897 次 |
最近记录: |