[英]C# XML Databinding Winforms
let me describe the situation. 让我描述一下情况。 Winforms C# Winforms C#
I have xml file with data. 我有带数据的xml文件。 I load this data to an user defined class object using Deserialize
. 我使用Deserialize
将此数据加载到用户定义的类对象。
Based on this object with data, I build [in Form] UI: many tabPages
of custom controls (textBox, 2 buttons in groupBox). 基于此带有数据的对象,我构建了[Form] UI:许多tabPages
( tabPages
, tabPages
的2个按钮)。 I can also save this user defined class object using Serialize
to XML file. 我还可以使用“ Serialize
XML”文件保存此用户定义的类对象。
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: 创建具有UI和数据对象之间的连接的UI:
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. 当保存回xml文件时,我仅获得本地源更新。 When I commented LocalSource binding then it works for RemoteSource. 当我评论LocalSource绑定时,它适用于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. 解决方案是在“自定义控件”和“ CustomClass”对象中建立LocalSource / RemoteSource的一对一连接。
So the solution is to enclose this two or more properties in one object. 因此解决方案是将这两个或多个属性包含在一个对象中。
additional class: 附加类:
public class Source
{
string sourceLocal;
string sourceRemote;
public Source()
{
sourceLocal = string.Empty;
sourceRemote = string.Empty;
}
public string SourceLocal
{
get { return sourceLocal; }
set { sourceLocal = value; }
}
public string SourceRemote
{
get { return sourceRemote; }
set { sourceRemote = value; }
}
public Source(string local,string remote)
{
SourceLocal = local;
SourceRemote = remote;
}
}
Then in previous classes: 然后在之前的课程中:
public class ActionGroup
{
public string type;
public string text;
Source source;
public ActionGroup()
{
this.type = string.Empty;
this.text = string.Empty;
this.source = new Source();
}
public Source Source
{
get { return source; }
set { source = value; }
}
}
And finally binding: 最后绑定:
view.DataBindings.Add("Source", actionGroup, "Source", true, DataSourceUpdateMode.OnPropertyChanged);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.