C#XML数据绑定Winforms
C#XML数据绑定Winforms,c#,xml,data-binding,C#,Xml,Data Binding,让我描述一下情况。Winforms C# 我有一个包含数据的xml文件。我使用反序列化将此数据加载到用户定义的类对象中 基于这个包含数据的对象,我构建了[in Form]UI:manytabPages自定义控件(textBox,groupBox中的两个按钮)。我还可以使用Serialize将这个用户定义的类对象保存到XML文件 问题: 用户类别: public class Layout { public string type; public List<TabPage&g
让我描述一下情况。Winforms C#
我有一个包含数据的xml文件。我使用
反序列化将此数据加载到用户定义的类对象中
基于这个包含数据的对象,我构建了[in Form]UI:manytabPages
自定义控件(textBox,groupBox中的两个按钮)。我还可以使用Serialize
将这个用户定义的类对象保存到XML文件
问题:
用户类别:
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; }
}
}
正在创建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);
}
}
我遇到的问题发生在这里:
view.DataBindings.Add("SourceLocal", actionGroup, "SourceLocal", true, DataSourceUpdateMode.OnPropertyChanged);
view.DataBindings.Add("SourceRemote", actionGroup, "SourceRemote", true, DataSourceUpdateMode.OnPropertyChanged);
我只能绑定一个属性。第一个有效。第二个不能正常工作。当保存回xml文件时,我只得到本地源代码更新。
当我评论LocalSource绑定时,它适用于RemoteSource。我想我把这些绑定传错了。但如何正确地做到这一点呢
解决方案是在自定义控件和CustomClass对象中连接一个LocalSource/RemoteSource。因此,解决方案是将这两个或多个属性包含在一个对象中
附加类别:
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;
}
}
然后在以前的课程中:
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; }
}
}
最后是有约束力的:
view.DataBindings.Add("Source", actionGroup, "Source", true, DataSourceUpdateMode.OnPropertyChanged);
何时以及如何调用ChangeToRemote/ChangeToLocal?你有没有检查过他们的名字?textBox1\u TextChanged做什么?@Ralf ChangeToRemote/Local通过复选框完成。在主窗体中选中。这是肯定的。数据在控件中更新,但未正确绑定到布局对象。textBox1_TextChanged为空(内部注释,在数据绑定试用前使用)
view.DataBindings.Add("Source", actionGroup, "Source", true, DataSourceUpdateMode.OnPropertyChanged);