让设计人员为UserControl的简单List属性生成AddRange

时间:2018-07-29 12:11:37

标签: c# visual-studio winforms user-controls windows-forms-designer

我创建了一个新的UserControl,如下所示:

    public partial class MyControl : UserControl {
    List<Fruit> _fruits = new List<Fruit>();

    public List<Fruit> Fruits {
        get {
            return _fruits;
        }
        set {
            _fruits = value;
        }
    }

    public UserControl1() {
        InitializeComponent();
    }
}

Fruit类仅包含两个get / set属性,而没有其他内容:

public class Fruit {
    public bool Edible {
        get;
        set;
    }

    public string Name {
        get;
        set;
    }
}

当我从窗体上的Visual Studio工具箱中拖动MyControl的实例,然后继续使用Visual Studio设计器将Fruit对象添加到MyControl的Fruits集合中时,我希望设计器能够生成新的Fruit实例,并通过生成对集合FruitsAddRange方法的调用,将它们自动添加到设计器生成的代码中的Add集合中。

但是,它不会生成任何将它们添加到AddRange的{​​{1}}集合中的MyControl代码,因此我最终在代码中“挥之不去” Fruits实例-背后。我已经尝试过将Fruit属性添加到[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]属性中,但是什么也没做。我想念什么?

1 个答案:

答案 0 :(得分:1)

[DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]对于Fruits是必需的,以便让设计者知道序列化其内容。此外,Fruits属性不需要公共设置器:

using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
public partial class MyControl : UserControl
{
    public MyControl()
    {
        Fruits = new List<Fruit>();
        InitializeComponent();
    }
    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)]
    public List<Fruit> Fruits { get; private set; }
}

结果将生成以下代码:

Sample.Fruit fruit1 = new Sample.Fruit();
Sample.Fruit fruit2 = new Sample.Fruit();
Sample.Fruit fruit3 = new Sample.Fruit();
this.myControl1 = new Sample.MyControl();
// 
// myControl1
// 
fruit1.Edible = true;
fruit1.Name = "Apple";
fruit2.Edible = true;
fruit2.Name = "Orange";
fruit3.Edible = true;
fruit3.Name = "Banana";
this.myControl1.Fruits.Add(fruit1);
this.myControl1.Fruits.Add(fruit2);
this.myControl1.Fruits.Add(fruit3);

清洁设计器生成的代码

如果要生成更简洁的代码,如下所示:

this.myControl1 = new Sample.MyControl();
// 
// myControl1
// 
this.myControl1.Fruits.Add(new Sample.Fruit(true, "Apple"));
this.myControl1.Fruits.Add(new Sample.Fruit(true, "Orange"));
this.myControl1.Fruits.Add(new Sample.Fruit(true, "Banana"));

您需要为Fruit类创建一个TypeConverter,该类使用InstanceDescriptor创建该类的实例:

using System;
using System.ComponentModel;
using System.ComponentModel.Design.Serialization;
using System.Globalization;
[TypeConverter(typeof(FruitConverter))]
public class Fruit
{
    public Fruit() { }
    public Fruit(bool edible, string name) : this()
    {
        Edible = edible;
        Name = name;
    }
    public bool Edible { get; set; }
    public string Name { get; set; }
}
public class FruitConverter : TypeConverter
{
    public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
    {
        if (destinationType == typeof(InstanceDescriptor)) return true;
        return base.CanConvertTo(context, destinationType);
    }
    public override object ConvertTo(ITypeDescriptorContext context, 
        CultureInfo culture, object value, Type destinationType)
    {
        if (destinationType == typeof(InstanceDescriptor)) {
            var ci = typeof(Fruit).GetConstructor(new Type[] { 
                typeof(bool), typeof(string) });
            var t = (Fruit)value;
            return new InstanceDescriptor(ci, new object[] { t.Edible, t.Name });
        }
        return base.ConvertTo(context, culture, value, destinationType);
    }
}
相关问题