Mam formularz (CustomerInfoForm) z 10 TextBoxami. Domyślna właściwość Text
dla każdego z TextBoxes
jest zdefiniowana w czasie projektowania. Podklasa CustomerInfoForm.CustomerInfo
zawiera właściwości do przechowywania danych wprowadzonych w formularzu. Podklasa zawierająca dane zostanie przekształcona do postaci XML.DataBinding do WinForm
W automatycznie wygenerowany kod postaci, każdy z pola tekstowe ma linię kodu w celu źródło danych w polu
this.customerInfoBindingSource = new System.Windows.Forms.BindingSource(this.components);
kodów generowanych automatycznie przez C# wewnątrz za każdym polu:
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";
(Zauważyłem, że właściwość Text
została ustawiona dopiero po DataBinding) w kodzie wygenerowanym przez IDE.
Kiedy uruchomić projekt, formularz jest wyświetlany z wartościami domyślnymi w TextBoxes
. Jednak po naciśnięciu klawisza SaveButton
w celu szeregowania właściwości w podklasie MyForm.CustomerInfo
, wszystkie są zerowe. Ponieważ te wartości zostaną zmienione tylko z formularza, miałem nadzieję, że nie będę musiał implementować interfejsu INotifyPropertyChanged
.
jestem brakuje czegoś podstawowe lub proste?
Kod do formularza tym serializacji danych jest załączony poniżej
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; }
}
}
}
EDIT - Dla innych, które mogą zdarzyć się w tej kwestii
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; }
}
}
}
Dzięki za odpowiedź. Poszedłem do wygenerowanego kodu okna i okazało się, że wiersz zawierający "DataSource" powinien być 'customerInfoForm_CustomerInfoBindingSource.DataSource = ci;' – DarwinIcesurfer