我需要以下问题的帮助:

我有一个具有两个属性的课程。

private byte m_selectedValue;
public byte SelectedValue
{
  get { return m_selectedValue; }
  set { m_selectedValue = value; }
}

private string[] m_possibleValues;
public string[] PossibleValues
{
  get { return m_possibleValues; }
  set { m_possibleValues = value; }
}

可能的价值存储可选值的列表。所选值包含实际选择值的索引。

在此状态下,属性编辑器显示所选值的索引。我想在属性网格中使用ComboBox选择该值,该属性网格与枚举属性相同的样式。 Combobox的列表将从可能的价值属性中填充。

在本文的帮助下(http://www.codeproject.com/kb/cpp/universaldropdowneditor.aspx)我设法创建了一个自定义编辑器,该编辑器显示了属性网格上的ComboBox,并具有从可能的Values属性中的值。我还可以选择该值,但仍然属性网格显示值的索引,而不是值本身。

这是编辑器的修改来源(原始来自Codeproject):

public class EnumParamValuesEditor : UITypeEditor
{
    private IWindowsFormsEditorService edSvc;

    public override UITypeEditorEditStyle GetEditStyle(System.ComponentModel.ITypeDescriptorContext context)
    {
        if ((context != null) && (context.Instance != null))
            return UITypeEditorEditStyle.DropDown;
        return UITypeEditorEditStyle.None;
    }

    public override object EditValue(System.ComponentModel.ITypeDescriptorContext context, IServiceProvider provider, object value)
    {
        if ((context == null) || (provider == null) || (context.Instance == null))
            return base.EditValue(provider, value);
        edSvc = (IWindowsFormsEditorService)provider.GetService(typeof(IWindowsFormsEditorService));
        if (edSvc == null)
            return base.EditValue(provider, value);
        ListBox lst = new ListBox();
        PrepareListBox(lst, context);
        lst.SelectedIndex = (byte)value;
        edSvc.DropDownControl(lst);
        if (lst.SelectedItem == null)
            value = null;
        else
            value = (byte)lst.SelectedIndex;
        return value;
    }

    private void PrepareListBox(ListBox lst, ITypeDescriptorContext context)
    {
        lst.IntegralHeight = true;
        string[] coll = ((EnumTerminalParam)context.Instance).PossibleValues;
        if (lst.ItemHeight > 0)
        {
            if ((coll != null) && (lst.Height / lst.ItemHeight < coll.Length))
            {
                int adjHei = coll.Length * lst.ItemHeight;
                if (adjHei > 200)
                    adjHei = 200;
                lst.Height = adjHei;
            }
        }
        else
            lst.Height = 200;
        lst.Sorted = true;
        FillListBoxFromCollection(lst, coll);
        lst.SelectedIndexChanged += new EventHandler(lst_SelectedIndexChanged);
    }

    void lst_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (edSvc == null)
            return;
        edSvc.CloseDropDown();
    }

    public void FillListBoxFromCollection(ListBox lb, ICollection coll)
    {
        lb.BeginUpdate();
        lb.Items.Clear();
        foreach (object item in coll)
            lb.Items.Add(item);
        lb.EndUpdate();
        lb.Invalidate();
    }

}

当然,它需要进一步的修改才能正确处理某些情况(例如,可能的价值为空)。

因此,是否可以在属性编辑器中显示可能的值[SelectedValue]而不是所选值?

有帮助吗?

解决方案

您需要将自定义的类型逆变器附加到所选价值属性上,并使可能的价值不可浏览。 TypeConverter将负责在PropertyGrid而不是INT中显示字符串。因此,基本上,您需要覆盖CanConvertto,canconvertto,转换和转换。当您想获取自定义字符串时,请使用传递给这些方法的上下文参数,并在目标实例中调用您的可能价值属性。那应该做到这一点。似乎您在这里不需要任何自定义UityPeeditor。

其他提示

而不是两个单独的属性,为什么不将它们绑在字典类型中。在这种情况下,它更容易使用。将您的索引作为密钥和字符串[]作为值。只是不要将自己限制为索引的字节。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top