c# – 无法创建自定义控件的复杂属性

我正在尝试创建多值复杂的自定义控件,但编写的代码不起作用,只有属性资源管理器中的多值只读属性.这是代码

private MyComboProperties _MyComboProperties;

public MyComboProperties MyComboPropertiesValues
{
    get
    {
        return _MyComboProperties;
    }
    set
    {
        _MyComboProperties = value;
    }
}

//MyComboProperties struct is like this

[System.Runtime.InteropServices.ComVisible(true)]
public struct MyComboProperties
{
    private string _MySourceQuery;
    private string _MyDisplayMember;
    private string _MyValueMember;

    public MyComboProperties(string mySourceQuery, string myDisplayMember, string myValueMember)
    {
        _MySourceQuery = mySourceQuery;
        _MyDisplayMember = myDisplayMember;
        _MyValueMember = myValueMember;
    }

    public string MySourceQuery
    {
        get
        {
            return _MySourceQuery;
        }
        set
        {
            _MySourceQuery = value;
        }
    }

    public string MyDisplayMember
    {
        get
        {
            return _MyDisplayMember;
        }
        set
        {
            _MyDisplayMember = value;
        }
    }

    public string MyValueMember
    { 
        get
        {
            return _MyValueMember;
        }
        set
        {
            _MyValueMember = value;
        }
    }
}

或者有人为我提供了具有3个输入字符串值的多值自定义控件的示例代码.

最佳答案 使用以下代码来实现您的目标.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Globalization;

namespace ExpandableMultiValuedCustomControl
{
public partial class MyComboBox : System.Windows.Forms.ComboBox
{
    private MyComboProperties _comboProperties = new MyComboProperties();
    public MyComboBox()
    {
        InitializeComponent();
    }

    public MyComboBox(IContainer container)
    {
        container.Add(this);

        InitializeComponent();
    }

    [Category("My Combo Properties")]
    [DisplayName("My Combo Properties")]
    [Description("My Combo Properties")]
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public MyComboProperties MyComboProperties
    {
        get
        {
            return _comboProperties;
        }
        set
        {
            _comboProperties = value;
        }
    }
}

[DisplayName("My Combo Properties")]
[Description("CMy Combo Properties")]
[DefaultProperty("Text")]
[DesignerCategory("Component")]
[TypeConverter(typeof(ExpandableObjectConverter))]

public class MyComboProperties
{
    private string _MySourceQuery;
    private string _MyDisplayMember;
    private string _MyValueMember;
    public MyComboProperties()
    {

    }

    [Category("MyComboBoxProperties")]
    [DisplayName("MySourceQuery")]
    [Description("MySourceQuery")]
    public string MySourceQuery
    {
        get
        {
            return _MySourceQuery;
        }
        set
        {
            _MySourceQuery = value;
        }
    }

    [Category("MyComboBoxProperties")]
    [DisplayName("MyDisplayMember")]
    [Description("MyDisplayMember")]
    public string MyDisplayMember
    {
        get
        {
            return _MyDisplayMember;
        }
        set
        {
            _MyDisplayMember = value;
        }
    }

    [Category("MyComboBoxProperties")]
    [DisplayName("MyValueMember")]
    [Description("MyValueMember")]
    public string MyValueMember
    {
        get
        {
            return _MyValueMember;
        }
        set
        {
            _MyValueMember = value;
        }
    }
    }
}

要访问这些属性,您必须像这样访问:

myComboBox1.MyComboProperties.MyDisplayMember 
myComboBox1.MyComboProperties.MyValueMember 
myComboBox1.MyComboProperties.MySourceQuery
点赞