Property edit style as DropDown in a VS .NET custom component

我的梦境 提交于 2021-02-05 06:58:05

问题


I'd like to use the functionality of a ComboBox as edit option for a var in the properties window of a custom control / component. Not the ComboBox component itself.

As example:

private string[] _stringArray = { "string0", "string1" };
public string[] StringArray
{
    get { return _stringArray; }
    //callback
    //set { _stringArray = value; }
}

As you know this will give me the object browser as view/edit option in the property window. Funny thing that I can edit the values even with no setter.

In my researches I found out that it is possible ("UITypeEditorEditStyle.DropDown"). But I have no idea how to implement that. Or what [Instructions] I could set for the "StringArray".

My final goal is a copy of the object selector drop-down of visual studio as a property parameter:

With custom event handling of course. But as you see I'm far away to realize that. :(

I have been looking for a tutorial on the following topics for a long time:

  • [Designer] instructions reference
  • A basic tutorial how to manage the display style of properties

However I'm tired of my unsuccessful researches. Some good links are always welcome.


回答1:



UPDATE:

After I more or less understood the principle (from the link in the comments, thanks) I came to an interim solution. I realized that I need at least an int var to set a selected `index`. I thought / hoped that VS can do this automatically. Like it does with enums. And my lack of knowledge concerning [Instructions].

I could define a string variable as a placeholder for the selected index of the array in order to do without the TypeConverter, but that would make even less sense. I really don't need another abstract variable for nothing.

So the basis drop-down, which e.g. can display enums directly, does not appear to be applicable. So they use a trick with "UITypeEditorEditStyle.DropDown", which actually isn't a drop-down. It's just a button where you can place the control of your choice. In my case a ListView. Since the "drop" of the "down" already exists. Looks like cheating. ;)

//...
 
[TypeConverter(typeof(StringArrayConverter))]
public interface IStringArray
{
    int SelectedIndex { get; set; }
    string[] StringArray { get; set; }
}

public class DropDownStringArray : IStringArray
{
    private string[] _stringArray = { "string0", "string1", "string2", "string3", "string4", "string5", "string6" };
    public int SelectedIndex { get; set; }
    public string[] StringArray
    {
        get { return _stringArray; }
        set { _stringArray = value; }
    }
}

private DropDownStringArray _ddsa = new DropDownStringArray();
[Editor(typeof(StringArrayTypeEditor), typeof(UITypeEditor))]
public DropDownStringArray MyDropDownStringArray
{
    get { return _ddsa; }
    set { _ddsa = value; }
}
 
//...
 
public class StringArrayConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        return destinationType == typeof(string);
    }

    public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            var sa = value as IStringArray;
            if (sa != null) { return sa.StringArray[sa.SelectedIndex]; }
        }
        return "(none)";
    }
}

public class StringArrayTypeEditor : UITypeEditor
{
    private IWindowsFormsEditorService _editorService;

    public override UITypeEditorEditStyle GetEditStyle(ITypeDescriptorContext context) { return UITypeEditorEditStyle.DropDown; }

    public override object EditValue(ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        _editorService = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        DropDownStringArray ddsa = (DropDownStringArray)value;
        ListBox lb = new ListBox();
        lb.SelectionMode = SelectionMode.One;
        for (int i = 0; i < ddsa.StringArray.Length; i++) { lb.Items.Add(ddsa.StringArray[i]); }
        lb.SetSelected(ddsa.SelectedIndex, true);
        lb.SelectedValueChanged += OnListBoxSelectedValueChanged;
        _editorService.DropDownControl(lb);
        if (lb.SelectedItem != null) { ddsa.SelectedIndex = lb.SelectedIndex; }
        return value;
    }

    private void OnListBoxSelectedValueChanged(object sender, EventArgs e)
    {
        _editorService.CloseDropDown();
    }
}
 

Which actually copy the entire class just to change the SelectedIndex. The right thing would be to abuse the SelectedIndex and convert it to a string or something like that. I think I do not care about that anymore. Rather to catch some fresh air. ;)

Maybe that will help someone else.

Note: This is not a practical propose. As example SelectedIndex will not be updated if you change the (length) of the array. I've choosen string[] because it's a really basic and well known type. I am aware that my "program" has no real use. It was just about understanding the principle.



来源:https://stackoverflow.com/questions/64530002/property-edit-style-as-dropdown-in-a-vs-net-custom-component

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!