XML : C# XML Databinding Winforms


let me describe the situation. Winforms C#
I have xml file with data. I load this data to an user defined class object using Deserialize.
Based on this object with data, I build [in Form] UI: many tabPages of custom controls (textBox, 2 buttons in groupBox). I can also save this user defined class object using Serialize to XML file.

Question:

user class:

  public class Layout  {      public string type;      public List<TabPage> TabPageList;        public Layout()      {          this.TabPageList = new List<TabPage>();      }  }  public class TabPage  {      public string text;      public List<ActionGroup> ActionGroupList;        public TabPage()      {          this.ActionGroupList = new List<ActionGroup>();      }  }  public class ActionGroup  {      public string type;      public string text;      string sourceLocal;      string sourceRemote;        public ActionGroup()      {          this.type = string.Empty;          this.text = string.Empty;          this.SourceLocal = string.Empty;          this.SourceRemote = string.Empty;      }        public string SourceLocal      {          get { return sourceLocal; }          set { sourceLocal = value; }      }      public string SourceRemote      {          get { return sourceRemote; }          set { sourceRemote = value; }      }  }    

Custom control:

  public partial class ViewActionGroup : UserControl  {      public string type;      public string text;      string sourceLocal;      string sourceRemote;      public bool isRemote;      public bool isDone;        public ViewActionGroup()      {          this.type = string.Empty;          this.text = string.Empty;          this.SourceLocal = string.Empty;          this.SourceRemote = string.Empty;          this.isRemote = false;          this.isDone = false;          InitializeComponent();      }      public ViewActionGroup(ActionGroup actionGroup)      {          this.type = actionGroup.type;          this.text = actionGroup.text;          this.SourceLocal = actionGroup.SourceLocal;          this.SourceRemote = actionGroup.SourceRemote;          this.isRemote = false;          this.isDone = false;          InitializeComponent();            groupBox1.Text = text;          button1.Text = type;          button1.Click += new EventHandler(Button_Click);          textBox1.Text = SourceLocal;          textBox1.TextChanged += new EventHandler(textBox1_TextChanged);      }        public string SourceLocal      {          get { return sourceLocal; }          set { sourceLocal = value; }      }      public string SourceRemote      {          get { return sourceRemote; }          set { sourceRemote = value; }      }        public void ChangeToRemote()      {          this.SourceLocal = textBox1.Text;          isRemote = true;          textBox1.Text = this.SourceRemote;                  }        public void ChangeToLocal()      {          this.SourceRemote = textBox1.Text;          isRemote = false;          textBox1.Text = this.SourceLocal;                  }  }    

Creating UI with connection between UI and data object:

  private void CreateLayout(Layout layout)      {          this.Text = layout.type;          if (panelMain.Controls.Count>0)          {              panelMain.Controls.Clear();          }          TabControl tabControl = new TabControl();            tabControl.Dock = DockStyle.Fill;            int tabCount = 0;            foreach (TabPage tabpage in layout.TabPageList)          {              int actionCount = 0;                tabControl.TabPages.Add(tabpage.text);              foreach (ActionGroup actionGroup in tabpage.ActionGroupList)              {                  ViewActionGroup view = new ViewActionGroup(actionGroup);                  view.Location = new Point(0, actionCount * view.Height);                  view.DataBindings.Add("SourceLocal", actionGroup, "SourceLocal", true, DataSourceUpdateMode.OnPropertyChanged);                  view.DataBindings.Add("SourceRemote", actionGroup, "SourceRemote", true, DataSourceUpdateMode.OnPropertyChanged);                  tabControl.TabPages[tabCount].Controls.Add(view);                  tabControl.TabPages[tabCount].AutoScroll = true;                  tabControl.TabPages[tabCount].AutoScrollMinSize = new System.Drawing.Size(tabControl.Width/2,tabControl.Height);                  actionCount++;              }              tabCount++;              this.panelMain.Controls.Add(tabControl);          }        }    

The problem I get happens here:

  view.DataBindings.Add("SourceLocal", actionGroup, "SourceLocal", true, DataSourceUpdateMode.OnPropertyChanged);  view.DataBindings.Add("SourceRemote", actionGroup, "SourceRemote", true, DataSourceUpdateMode.OnPropertyChanged);    

I can bind only one property. The first one works. The second does not work properly. When saving back to xml file I get only local source update. When I commented LocalSource binding then it works for RemoteSource. I guess I pass those bindings wrong to control. But how to do it properly?

Solution would be to have on-to-one connection of LocalSource/RemoteSource in Custom Control and CustomClass object.

No comments:

Post a Comment