让我来描述一下情况。 Winforms C#
我有带数据的xml文件。我使用Deserialize
将此数据加载到用户定义的类对象
基于这个带有数据的对象,我在[Form] UI中构建:许多tabPages
自定义控件(textBox,groupBox中的2个按钮)。我还可以使用Serialize
将此用户定义的类对象保存到XML文件。
问题:
当我在自定义控件中的表单UI中更新textBox.Text
时,我不知道如何使用数据(布局布局)保持与对象的连接,并将更新后的对象与数据保存到XML。文本的更改仅在用户自定义控件TextBox中发生。我想在数据对象(布局)中更新UI中的数据,然后使用Serialization
保存。
用户类:
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;
public string sourceLocal;
public string sourceRemote;
public ActionGroup()
{
this.type = string.Empty;
this.text = string.Empty;
this.sourceLocal = string.Empty;
this.sourceRemote = string.Empty;
}
}
自定义控制:
public partial class ViewActionGroup : UserControl
{
public string type;
public string text;
public string sourceLocal;
public 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 void ChangeToRemote()
{
isRemote = true;
textBox1.Text = this.sourceRemote;
}
public void ChangeToLocal()
{
isRemote = false;
textBox1.Text = this.sourceLocal;
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
if (this.isRemote)
{
this.sourceRemote = textBox1.Text;
}
else
{
this.sourceLocal = textBox1.Text;
}
}
创建用户界面和数据对象之间的连接松散的用户界面:
private void CreateLayout(Layout layout)
{
this.Text = layout.type;
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);
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);
}
}
答案 0 :(得分:1)
有两种常见方式:
一个是例程WriteDataIntoControls
,另一个是ReadDataFromControls
,您可以手动将数据传输到可见控件和从可见控件传输数据(优点:最高控制度)。在这种情况下,您必须从XML源读取对象,将其反序列化到业务对象中,并创建所有可见控件及其值。保存后,您必须将所有值都转移到您的业务对象中,然后再将其展开。
第二个是DataBinding(优势:最高自动化程度)。请在此处阅读:https://msdn.microsoft.com/en-us/library/ef2xyb33%28v=vs.110%29.aspx?f=255&MSPPError=-2147217396
您可以绑定到简单值以及列表(包括导航)或复杂对象。
您可以在此处找到教程:http://www.codeproject.com/Articles/24656/A-Detailed-Data-Binding-Tutorial
答案 1 :(得分:1)
@Shnugo感谢您的反馈。你发布的教程没有帮助,因为它对我来说太难了,但Data Binding主题给了我一些线索。
这里简单易懂的VB教程实际上很简单。它帮助我在C#中快速完成。 https://www.youtube.com/watch?v=jqLQ2K9YY2A
C#解决方案
class MyObject
{
string name;
public MyObject()
{ }
public string Name
{
get { return name;}
set { name = value; }
}
}
public partial class Form1 : Form
{
MyObject obj = new MyObject();
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
obj.Name = "Lucas";
textBox1.DataBindings.Add("Text", obj, "Name", true, DataSourceUpdateMode.OnPropertyChanged);
}
private void button1_Click(object sender, EventArgs e)
{
label1.Text = obj.Name;
}
}