2011-06-18 15 views
6

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; } 
     } 
    } 
} 

Odpowiedz

3

Przede wszystkim trzeba aby zdecydować, czy bezpośrednio korzystać z funkcji databinding lub manipulować własnością Text. Te dwa podejścia nie powinny być mieszane razem.

Jeśli chcesz używać wiązania z danymi niż brakuje jednej linii kodu:

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

Trzeba pozwolić customerInfoBindingSource wiedzieć o źródle danych.

Po dodaniu tej linii, Text przypisany w czasie projektowania zostanie nadpisany przez tekst z powiązanego źródła danych. Jeśli chcesz użyć wiązania, musisz manipulować przy użyciu źródła danych, zamiast bezpośrednio ustawiać pola Text. W ten sposób:

public Form1() 
{ 
    InitializeComponent(); 

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

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