4

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

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 个输入字符串值的多值自定义控件的示例代码。

4

1 回答 1

2

使用下面的代码来实现您的目标。

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
于 2013-08-30T05:42:20.557 回答