Tengo un formulario (CustomerInfoForm) con 10 TextBoxes. La propiedad predeterminada Text
para cada uno de los TextBoxes
se define en tiempo de diseño. Una subclase CustomerInfoForm.CustomerInfo
contiene propiedades para contener los datos ingresados en el formulario. La subclase que contiene los datos se serializará a XML.DataBinding a WinForm
En el código de formulario generado de forma automática, cada uno de los cuadros de texto tiene una línea de código para enlazar la fuente de datos para el cuadro de texto
this.customerInfoBindingSource = new System.Windows.Forms.BindingSource(this.components);
Código generada automáticamente por el C# IDE para cada cuadro de texto:
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";
(Me di cuenta de que la propiedad Text
no está configurada hasta después del enlace de datos) en el código generado IDE.
Cuando ejecuto el proyecto, el formulario se muestra con los valores predeterminados en el TextBoxes
. Sin embargo, cuando se presiona SaveButton
para serializar las propiedades en la subclase MyForm.CustomerInfo
, todas son nulas. Dado que estos valores solo se cambiarán del formulario, esperaba que no tuviera que implementar la interfaz INotifyPropertyChanged
.
¿Me falta algo básico o simple?
El código para el formulario incluyendo la serialización de los datos se adjunta a continuación
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; }
}
}
}
EDITAR - Para otros que pueden suceder a esta pregunta
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; }
}
}
}
Gracias por la respuesta. Ingresé al código generado de la forma de Windows y descubrí que la línea que contiene 'DataSource' debe ser 'customerInfoForm_CustomerInfoBindingSource.DataSource = ci;' – DarwinIcesurfer