WPF-在propertyGrid(DesignMode)处以组合方式显示对象的属性列表

时间:2019-07-19 10:10:36

标签: wpf debugging typeconverter .net-4.6 designmode

感谢您可能给我的答案。

我有一个用户在设计时插入的区域(及其属性)列表(VS默认propertyGrid)。 我需要在VS默认propertyGrid中显示一个组合,其中包含用户插入的区域以选择默认区域。

所有这些都是通过WPF和C#的控制。

我无法生成组合或显示用户插入的区域。

我也无法在设计模式下对其进行调试。

如果用户插入新区域,而无需重新编译解决方案,我需要自动重新加载组合。

我正在使用.Net 4.6。

非常感谢您能为我提供的所有帮助。

public class Zona
{
    private string m_nombre;
    private int m_id;

    public Zona() { }

    public Zona(string n, int i)
    {
        m_nombre = n;
        m_id = i;
    }

    public string Nombre
    {
        get => m_nombre;
        set => m_nombre = value;
    }

    public int Id
    {
        get => m_id;
        set => m_id = value;
    }

    public string Descripcion { get; set; }

    public override string ToString()
    {
        return string.Format("{0},{1}", m_nombre, m_id);
    }

    public static Zona Parse(string zonaString)
    {
        if (string.IsNullOrEmpty(zonaString))
        {
            return new Zona();
        }

        // The parts array holds the real and 
        // imaginary parts of the object.
        var parts = zonaString.Split(',');
        return new Zona((parts[0].Trim()), int.Parse(parts[1].Trim()));
    }
}

public class ZonaTypeConverter : TypeConverter
{
    public static List<Zona> defaultValues = new List<Zona>();


    static ZonaTypeConverter()
    {
        // Estos son los datos que se mostrarán en el combo
        defaultValues.Add(new Zona("Opcion1", 0));
        defaultValues.Add(new Zona("Opcion2", 1));
        defaultValues.Add(new Zona("Opcion3", 2));
        defaultValues.Add(new Zona("Opcion4", 3));
        defaultValues.Add(new Zona("Opcion5", 4));
        defaultValues.Add(new Zona("Opcion6", 5));
    }

    // Override CanConvertFrom to return true for String-to-Complex conversions.
    public override bool CanConvertFrom(
        ITypeDescriptorContext context,
        Type sourceType)
    {
        if (sourceType == typeof(string))
        {
            return true;
        }
        return base.CanConvertFrom(context, sourceType);
    }

    // Override CanConvertTo to return true for Complex-to-String conversions.
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(string))
        {
            return true;
        }
        return base.CanConvertTo(context, destinationType);
    }

    // Override ConvertFrom to convert from a string to an instance of Complex.
    public override object ConvertFrom(
        ITypeDescriptorContext context,
        System.Globalization.CultureInfo culture,
        object value)
    {
        var text = value as string;

        if (text != null)
        {   
            return Zona.Parse(text);
        }
        return base.ConvertFrom(context, culture, value);
    }

    // Override ConvertTo to convert from an instance of Complex to string.
    public override object ConvertTo(
        ITypeDescriptorContext context,
        System.Globalization.CultureInfo culture,
        object value,
        Type destinationType)
    {
        if (destinationType == null)
        {
            throw new ArgumentNullException("destinationType");
        }

        //Convert Complex to a string in a standard format.
        var c = value as Complex;

        if (c != null && CanConvertTo(context, destinationType))
        {
            return c.ToString();
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }

    public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
    {
        return true;
    }

    public override TypeConverter.StandardValuesCollection GetStandardValues(
        ITypeDescriptorContext context)
    {
        ComplexNumberControl num = (ComplexNumberControl)context.Container;
        // defaultValues = num.ListaZonas;

        var svc = new StandardValuesCollection(null);

        if (num is null)
        {
            svc = new StandardValuesCollection(defaultValues);
        }
        else
        {
            svc = new StandardValuesCollection(num.ListaZonas);
        }

        //defaultValues.Add(new Zona("prueba", 10));
        //defaultValues.Add(new Zona("Op4564531233cion1", 0));

        return svc;
    }
}

在xAML.cs中:

public partial class ComplexNumberControl : UserControl
{
    public ComplexNumberControl()
    {
        InitializeComponent();
    }

    public Complex ComplexNumber
    {
        get => (Complex)GetValue(ComplexNumberProperty);

        set => SetValue(ComplexNumberProperty, value);
    }

    public Zona ZonaSeleccionada
    {
        get => (Zona)GetValue(ZonaSeleccionadaProperty);

        set => SetValue(ZonaSeleccionadaProperty, value);
    }

    public List<Zona> ListaZonas
    {
        get
        {
           return (List<Zona>)GetValue(ListaZonasProperty);
        }
        set => SetValue(ListaZonasProperty, value);
    }


    private List<Zona> _ListZonas = new List<Zona>();

    public static readonly DependencyProperty ComplexNumberProperty = DependencyProperty.Register(
      "ComplexNumber",
      typeof(Complex),
      typeof(ComplexNumberControl),
      new PropertyMetadata(new Complex()));

    public static readonly DependencyProperty ZonaSeleccionadaProperty = DependencyProperty.Register(
     "ZonaSeleccionada",
     typeof(Zona),
     typeof(ComplexNumberControl),
     new PropertyMetadata(new Zona()));

    public static readonly DependencyProperty ListaZonasProperty = DependencyProperty.Register(
   "ListaZonas",
   typeof(List<Zona>),
   typeof(ComplexNumberControl),
   new PropertyMetadata(new List<Zona>()));
}

如您所见,在ZonaTypeConverter类中,我有一个默认列表:

defaultValues.Add (new Zone ("Option1", 0));
defaultValues.Add (new Zone ("Option2", 1));
defaultValues.Add (new Zone ("Option3", 2));
defaultValues.Add (new Zone ("Option4", 3));
defaultValues.Add (new Zone ("Option5", 4));
defaultValues.Add (new Zone ("Option6", 5));

如果在“公共重写TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext上下文)”中,我直接执行“ svc = new StandardValuesCollection(defaultValues);”组合正确生成。

但是,如果我将“ defaultValues”设置为“ num.ListZones”,则不会生成该组合。

Example img

0 个答案:

没有答案