9

カスタム UITypeEditor を作成した特定の型のすべてのインスタンスに EditorAttribute を配置することは避けたいと思います。

ソースを変更できないため、タイプに EditorAttribute を配置できません。

使用される唯一の PropertyGrid インスタンスへの参照があります。

PropertyGrid インスタンス (またはすべてのインスタンス) に、特定のタイプに遭遇するたびにカスタム UITypeEditor を使用するように指示できますか?

これは、.NET 2.0 以降でこれを行う方法の開始点を提供する MSDN の記事です

4

3 に答える 3

3

Marcのソリューションは、EditorAttributeをBarタイプにグローバルに適用します。デリケートな性質がある場合は、特定のインスタンスのプロパティに注釈を付けることをお勧めします。残念ながら、それは不可能です TypeDescriptor.AddAttributes

ViewModel<T>私の解決策は、Tからプロパティをコピーし、一部に追加の属性で注釈を付けるラッパーを作成することでした。datumReport型の変数があるとすると、次のように使用します。

        var pretty = ViewModel<Report>.DressUp(datum);
        pretty.PropertyAttributeReplacements[typeof(Smiley)] = new List<Attribute>() { new EditorAttribute(typeof(SmileyEditor),typeof(UITypeEditor))};
        propertyGrid1.SelectedObject = pretty;

ViewModel<T>定義されている場所:

public class ViewModel<T> : CustomTypeDescriptor
{
    private T _instance;
    private ICustomTypeDescriptor _originalDescriptor;
    public ViewModel(T instance, ICustomTypeDescriptor originalDescriptor) : base(originalDescriptor)
    {
        _instance = instance;
        _originalDescriptor = originalDescriptor;
        PropertyAttributeReplacements = new Dictionary<Type,IList<Attribute>>();
    }

    public static ViewModel<T> DressUp(T instance)
    {
        return new ViewModel<T>(instance, TypeDescriptor.GetProvider(instance).GetTypeDescriptor(instance));
    }

    /// <summary>
    /// Most useful for changing EditorAttribute and TypeConvertorAttribute
    /// </summary>
    public IDictionary<Type,IList<Attribute>> PropertyAttributeReplacements {get; set; } 

    public override PropertyDescriptorCollection GetProperties (Attribute[] attributes)
    {
        var properties = base.GetProperties(attributes).Cast<PropertyDescriptor>();

        var bettered = properties.Select(pd =>
            {
                if (PropertyAttributeReplacements.ContainsKey(pd.PropertyType))
                {
                    return TypeDescriptor.CreateProperty(typeof(T), pd, PropertyAttributeReplacements[pd.PropertyType].ToArray());
                }
                else
                {
                    return pd;
                }
            });
        return new PropertyDescriptorCollection(bettered.ToArray());
    }

    public override PropertyDescriptorCollection GetProperties()
    {
        return GetProperties(null);
    }
}

上で定義したように、これは特定のタイプのプロパティを置き換えますが、より高い解像度が必要な場合は、名前でプロパティを置き換えることができます。

于 2012-09-25T16:06:51.880 に答える
0

クラスにEditor 属性を追加するだけです。

于 2009-05-11T17:51:02.363 に答える