简体   繁体   English

如何将自定义属性添加到UserControl,在设计时显示为组合框

[英]How to add a custom property to a UserControl which display as a combo box at the design time

Edit: The question is about adding a property during the design time , not during the runtime . 编辑:问题是在设计时添加属性,而不是在运行时

I created a UserControl, in which contains a button, and I want to add a new property that when user uses it in form, he could see the property FillColor in user control's Property Tab . 我创建了一个UserControl,其中包含一个按钮,我想添加一个新属性,当用户在表单中使用它时,他可以在用户控件的Property Tab看到属性FillColor What's more, that property should be in the form of a combobox, which allows user to select the Color in System.Drawing.Color. 更重要的是,该属性应该采用组合框的形式,允许用户在System.Drawing.Color中选择Color

For Example: 例如:

I call my UserControl HalfFill . 我打电话给我的UserControl HalfFill What it does is to fill a normal button from start to the half of he size. 它的作用是从正常按钮填充正常按钮到其大小的一半。

Now I want 2 properties to be customized using Property Tab : 现在我想使用Property Tab自定义2个属性:

  • the first half color 上半部分颜色
  • the second half color 下半部分颜色

And when user chooses the color , the choices are listed in combobox (behaves like BackColor property for normal buttons), and all of them come from System.Drawing.Color. 当用户选择颜色时,选项列在组合框中(行为类似于普通按钮的BackColor属性),并且所有选项都来自System.Drawing.Color。

I want to do it with Enum. 我想和Enum一起做。 Can someone help me? 有人能帮我吗? How i put the comboxBox with its values into the Properties Tab ? 我如何将comboxBox及其值放入Properties Tab

Here is an example that I am looking for 这是我正在寻找的一个例子

public partial class HalfButton: UserControl
{
    public ComboBox ChooseColor //of all color in System.Drawing.Color
    {
        /* must be in that comboBox all the color in Color library so the user could choose 
        from properies of the button the color he want */

        get { return x.Item[colorfill.ToString()); }
        set { colorfill = Color.FromName(value); OnPaint(null); }
    }

    private Color colorfill = Color.Tomato;

    ComboBox x;

    public HalfButton()
    {
        InitializeComponent();

        x = new ComboBox();
        ArrayList ColorList = new ArrayList();
        Type colorType = typeof(System.Drawing.Color);
        PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static |
                                                              BindingFlags.DeclaredOnly |
                                                              BindingFlags.Public);
        foreach (PropertyInfo c in propInfoList)
        {
            x.Items.Add(c.Name);
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        MessageBox.Show(colorfill+""); 
    }
}

following code will give you an idea how you can bind the combo using System.Drawing.Color. 下面的代码将告诉您如何使用System.Drawing.Color绑定组合。 Also need to use namespace using System.Reflection; 还需要使用System.Reflection来使用命名空间;

ArrayList ColorList = new ArrayList();
        Type colorType = typeof(System.Drawing.Color);
        PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
        foreach (PropertyInfo c in propInfoList)
        {
            this.comboBox1.Items.Add(c.Name);
        }

That my last version... now i only need to bind the comboBox values (that in the combBox) to the properties when i try that it show 那是我的最后一个版本...现在我只需要在尝试显示它时将comboBox值(在combBox中)绑定到属性

ChooseColor1 | ChooseColor1 | None 没有

    public static ComboBox ComboFill()
             {
                 ComboBox xx = new ComboBox();
                 ArrayList ColorList = new ArrayList();
                 Type colorType = typeof(System.Drawing.Color);
                 PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
                 foreach (PropertyInfo c in propInfoList)
                 {
                     xx.Items.Add(c.Name);
                 }
                 return xx;
             }
           private Color colorfill1 = Color.Tomato;
           private Color colorfill2 = Color.Tomato;
           private ComboBox x1=ComboFill();
           private ComboBox x2=ComboFill();

     public ComboBox ChooseColor1 {

            get { return x1; }
            set { x1=value; OnPaint(null); }

        }
     public ComboBox ChooseColor2 {

            get { return x2; }
            set { x2=value; OnPaint(null); }

        }

     protected override void OnPaint(PaintEventArgs e)
            {
                    //my functions
//something with colorfill1
//something with colorfill2


            }

Do you mean the property in the Designer ? 你的意思是Designer的财产吗? Please take a look at my example below. 请看下面我的例子。

I created a UserControl called ColorTextBox , and defined a property called FilledColor . 我创建了一个名为ColorTextBoxUserControl ,并定义了一个名为FilledColor的属性。 In the designer, I am able to change the color in a combo box: 在设计器中,我可以更改组合框中的颜色:

The property is shown in the Property Tab : 该属性显示在“ Property Tab

该属性显示在“属性”选项卡中

Colors in the combobox, Drawing.Colors are under Web tab: 组合框中的颜色,Drawing.Colors在Web选项卡下:

ComboBox中的颜色

After change the FilledColor to Highlight : FilledColor更改为Highlight

改变之后

My code is fairly simple: 我的代码很简单:

private Color _filledColor;

public ColorTextBox()
{
    InitializeComponent();
    _filledColor = Color.FromKnownColor(KnownColor.Control);
}

public Color FilledColor
{
    get { return _filledColor; }
    set
    {
        _filledColor = value;
        button1.BackColor = _filledColor;
    }
}

The only change is to change your property type to Color, and the magic completed. 唯一的变化是将您的属性类型更改为Color,并完成魔术。

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

相关问题 UserControl 依赖属性设计时间 - UserControl Dependency Property design time 将自定义属性添加到UserControl - Add a custom property to a UserControl WPF C#Custom UserControl如何添加接受绑定的属性 - WPF C# Custom UserControl How To add a Property which accepts an Binding 将属性附加到usercontrol并在设计时更新它 - Attach property to usercontrol and updating it at design time 如何使用组合框/下拉列表显示 DataGrid 属性,该组合框/下拉列表根据另一个文本框属性的输入而变化 - How to display a DataGrid property with combo box/drop down list that varies according to input of another text box property 如何将值和文本绑定到属性网格中存在的下拉列表(组合框)? - How to bind value and text to a drop down (combo box) which is present in a property grid? 如何在设计时获取WPF UserControl更新? - How to get WPF UserControl to update at design time? 在设计时如何使UserControl保持惰性? - How to make a UserControl inert at design time? 如何在WPF中制作自定义组合框? - How to make a custom combo box in wpf? 如何在运行时单击下拉列表之前静态设置组合框的显示成员 - how to set display member of combo box statically before clicking drop down list at run time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM