[英]C# XML Databinding Winforms
讓我描述一下情況。 Winforms C#
我有帶數據的xml文件。 我使用Deserialize
將此數據加載到用戶定義的類對象。
基於此帶有數據的對象,我構建了[Form] UI:許多tabPages
( tabPages
, tabPages
的2個按鈕)。 我還可以使用“ 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; }
}
}
自定義控件:
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;
}
}
創建具有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);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.