繁体   English   中英

具有结构值的C#数据绑定

[英]c# data binding with a structure value

关于我正在建造的驾驶员,我需要一些帮助。 我的静态类中有一些数据的结构。 必须从我的驱动程序类外部操纵此结构对象的数据。 在我的课堂上,我必须准备一些文本框,可以在课堂外分配和使用它们。 每个结构值成为一个文本框。 现在的问题是,我必须将此动态可变结构值与相应的textBox连接起来。 我必须使用dataBinding,因为将不得不使用大量数据。

请检查以下代码段以了解:

    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
}

感谢帮助!

我认为有可能做到如下。 定义您的班级:

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;
}

然后在表单上创建文本框:

var textBox1 = new TextBox { Parent = this };
var textBox2 = new TextBox { Parent = this, Top = 30 };

创建您的类的实例,并将这些文本框传递给它:

var driver = new Driver(textBox1, textBox2);
driver.ModuleName = "foo";
driver.DynamicNumber = "bar";
// data will be appear in the textboxes

有用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM