简体   繁体   English

将Winform绑定到字典或键/值列表

[英]Binding a Winform to a Dictionary or Key/Value List

I have a Winforms application that dynamically instantiates external form objects for use in a configuration UI. 我有一个Winforms应用程序,该应用程序动态实例化用于配置UI的外部表单对象。 Each dynamically-instantiated form is placed in a TabPage. 每个动态实例化的表单都放置在TabPage中。

I would like to create a property that will accept a Dictionary or Key/Value list, and populate the form with those values, matching up the value of the control with the value of the Key in the dictionary. 我想创建一个将接受Dictionary或Key / Value列表的属性,并使用这些值填充表单,以将控件的值与Dictionary中Key的值进行匹配。 Conversely, the property will also return a Key/Value list that corresponds to the values of the controls in the Winform. 相反,该属性还将返回一个与Winform中控件的值相对应的键/值列表。

Is this a simple binding problem, or do I need to write custom code? 这是一个简单的绑定问题,还是我需要编写自定义代码? What would that code look like? 该代码是什么样的?

In the past, I have accomplished this by writing a DTO class, using XML serialization and deserialization to persist the class's data, and hooking up the form fields to the DTO object's fields. 过去,我通过编写DTO类,使用XML序列化和反序列化来持久化类的数据,并将表单字段与DTO对象的字段挂钩来实现此目的。 The problem is, since the form is dynamically instantiated from an external DLL, there's no way to "reference" this DTO class statically, so I'm looking for a more dynamic approach. 问题是,由于窗体是从外部DLL动态实例化的,因此无法静态“引用”此DTO类,因此我正在寻找一种更动态的方法。

Turned out to be easier than I thought. 原来比我想象的要容易。

public Dictionary<string, string> Values
{
    get
    {
        var values = new Dictionary<string, string>();
        foreach (var control in Controls)
        {
            switch(control.GetType().Name) 
            {
                case "TextBox" : 
                    var textBox = (TextBox)control;
                    values.Add(textBox.Name, textBox.Text);
                    break;
                case "ComboBox":
                    var comboBox = (ComboBox)control;
                    values.Add(comboBox.Name, comboBox.SelectedItem.ToString());
                    break;
                case "CheckBox":
                    var checkBox = (CheckBox)control;
                    values.Add(checkBox.Name, checkBox.Checked.ToString());
                    break;

                // TODO: Add additional cases to support other control types
            }
        }
        return values;
    }
    set
    {
        foreach (var control in Controls)
        {
            switch (control.GetType().Name)
            {
                case "TextBox":
                    var textBox = (TextBox)control;
                    textBox.Text = value[textBox.Name];
                    break;
                case "ComboBox":
                    var comboBox = (ComboBox)control;
                    comboBox.SelectedItem = value[comboBox.Name];
                    break;
                case "CheckBox":
                    var checkBox = (CheckBox)control;
                    checkBox.Checked = bool.Parse(value[checkBox.Name]);
                    break;

                // TODO: Add additional cases to support other control types
            }
        }
    }

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

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