[英]c# data binding with a structure value
I need some help regarding a driver I`m building. 关于我正在建造的驾驶员,我需要一些帮助。 I have a structure of some data in my static class.
我的静态类中有一些数据的结构。 The data of this structure object has to be manipulated from outside my driver class.
必须从我的驱动程序类外部操纵此结构对象的数据。 Into my class I have to prepare some textBoxes, which can be assigned and used from outside the class.
在我的课堂上,我必须准备一些文本框,可以在课堂外分配和使用它们。 Each structure value becomes one textBox.
每个结构值成为一个文本框。 Now my problem is, I have to connect this dynamic changable structure values with the corresponding textBox.
现在的问题是,我必须将此动态可变结构值与相应的textBox连接起来。 I have to use dataBinding, cause there will be a huge amount of data I have to use.
我必须使用dataBinding,因为将不得不使用大量数据。
Pls check out the following code snippet for understanding: 请检查以下代码段以了解:
public static class driver
{
#region " data preparation "
//structure definition
public struct _data
{
public string moduleName;
public string dynamicNumber1;
//...
}
//instance object of struct
private _data moduleData = new _data();
//get;set property
public _data pModuleData
{
get
{
return moduleData;
}
set
{
moduleData = value;
}
}
#endregion
//build data binding(s) for each single "moduleData.structureItem"
//???????????????????? moduleData_itemBinding_ModuleName
//???????????????????? moduleData_itemBinding_dynamicNumber1
//...
#region " form elements preparation for external assignments "
//instance of forms objects, data can be assigned and used outside of this public static class
public static System.Windows.Forms.TextBox textBox_ModuleName = new System.Windows.Forms.TextBox();
public static System.Windows.Forms.TextBox textBox_dynamicNumber1 = new System.Windows.Forms.TextBox();
#endregion
#region " class initialisation "
static driver()
{
// class initialisation part
textBox_ModuleName.DataBindings = moduleData_itemBinding_ModuleName; //assign databindings from above ???????????
textBox_ModuleName.DataBindings = moduleData_itemBinding_dynamicNumber1; //adding databindings from above ???????????
}
#endregion
}
Thanks for help! 感谢帮助!
I think it is possible to make as follows. 我认为有可能做到如下。 Define your class:
定义您的班级:
public class Driver
{
public Driver(TextBox moduleName, TextBox dynamicNumber)
{
textBox_ModuleName = moduleName;
textBox_DynamicNumber = dynamicNumber;
textBox_ModuleName.DataBindings.Add("Text", this, "ModuleName");
textBox_DynamicNumber.DataBindings.Add("Text", this, "DynamicNumber");
}
public string ModuleName { get; set; }
public string DynamicNumber { get; set; }
private TextBox textBox_ModuleName;
private TextBox textBox_DynamicNumber;
}
Then create textboxes on the form: 然后在表单上创建文本框:
var textBox1 = new TextBox { Parent = this };
var textBox2 = new TextBox { Parent = this, Top = 30 };
Create instance of your class and pass to it these textboxes: 创建您的类的实例,并将这些文本框传递给它:
var driver = new Driver(textBox1, textBox2);
driver.ModuleName = "foo";
driver.DynamicNumber = "bar";
// data will be appear in the textboxes
It works. 有用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.