3

カスタム WinForms ホスティング環境を作成しました。ツールボックスと PropertyGrid があります。

ツールボックスに表示されるコントロールは、既存の WinForm コントロールから継承されます。

ドロップダウン リスト ソース:

public interface IPropertyFilter : ICustomTypeDescriptor
{
    PropertyDescriptorCollection FilterProperties(PropertyDescriptorCollection pdc);
    List<string> GetPropertiesToShow();
}

[Serializable]
public class DropDownList : System.Windows.Forms.ComboBox, IPropertyFilter
{
    public DropDownList()
    {
    }

    #region IPropertyFilter Members

    public TypeConverter GetConverter()
    {
        return TypeDescriptor.GetConverter(this, true);
    }

    public EventDescriptorCollection GetEvents(Attribute[] attributes)
    {
        return TypeDescriptor.GetEvents(this, attributes, true);
    }

    EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()
    {
        return TypeDescriptor.GetEvents(this, true);
    }

    public string GetComponentName()
    {
        return TypeDescriptor.GetComponentName(this, true);
    }

    public object GetPropertyOwner(PropertyDescriptor pd)
    {
        return this;
    }

    public AttributeCollection GetAttributes()
    {
        return TypeDescriptor.GetAttributes(this, true);
    }

    public PropertyDescriptorCollection GetProperties(Attribute[] attributes)
    {
        PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this, attributes, true);
        return FilterProperties(pdc);
    }

    PropertyDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetProperties()
    {
        PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(this, true);
        return FilterProperties(pdc);
    }

    public object GetEditor(Type editorBaseType)
    {
        return TypeDescriptor.GetEditor(this, editorBaseType, true);
    }

    public PropertyDescriptor GetDefaultProperty()
    {
        return TypeDescriptor.GetDefaultProperty(this, true);
    }

    public EventDescriptor GetDefaultEvent()
    {
        return TypeDescriptor.GetDefaultEvent(this, true);
    }

    public string GetClassName()
    {
        return TypeDescriptor.GetClassName(this, true);
    }

    public PropertyDescriptorCollection FilterProperties(PropertyDescriptorCollection pdc)
    {
        // Filter out properties that we do not want to display in PropertyGrid
        return ControlDesignerHelper.GetBrowsableProperties(pdc, GetPropertiesToShow());
    }

    // Determines what properties of this control has to be shown in PropertyGrid
    public List<string> GetPropertiesToShow()
    {
        // get a list of common properties that we want to show for all controls
        List<string> browsableProps = ControlDesignerHelper.GetBasePropertiesToShow();
        // add properties that are specific to this controls
        browsableProps.Add("Items");
        browsableProps.Add("AutoPostBack");
        browsableProps.Add("AppendDataBoundItems");
        browsableProps.Add("DataTextField");
        browsableProps.Add("DataValueField");
        return browsableProps;
    }

    #endregion
}

ICustomTypeDescriptorに表示したくないプロパティを除外するために実装しましたPropertyGrid

問題:

クラスから継承されたEnabled&プロパティの値をシリアル化する際に問題に直面しています。VisibleSystem.Windows.Forms.Control

WriteProperties メソッド ( BasicDesignerLoader):

private void WriteProperties(XmlDocument document, PropertyDescriptorCollection properties, object value, XmlNode parent, string elementName)
{
    foreach (PropertyDescriptor prop in properties)
    {
        System.Diagnostics.Debug.WriteLine(prop.Name);

        if (prop.ShouldSerializeValue(value))
        {
            string compName = parent.Name;
            XmlNode node = document.CreateElement(elementName);
            XmlAttribute attr = document.CreateAttribute("name");

            attr.Value = prop.Name;
            node.Attributes.Append(attr);

            DesignerSerializationVisibilityAttribute visibility = (DesignerSerializationVisibilityAttribute)prop.Attributes[typeof(DesignerSerializationVisibilityAttribute)];

            switch (visibility.Visibility)
            {
                case DesignerSerializationVisibility.Visible:
                    if (!prop.IsReadOnly && WriteValue(document, prop.GetValue(value), node))
                    {
                        parent.AppendChild(node);
                    }

                    break;

                case DesignerSerializationVisibility.Content:
                    object propValue = prop.GetValue(value);

                    if (typeof(IList).IsAssignableFrom(prop.PropertyType))
                    {
                        WriteCollection(document, (IList)propValue, node);
                    }
                    else
                    {
                        PropertyDescriptorCollection props = TypeDescriptor.GetProperties(propValue, propertyAttributes);

                        WriteProperties(document, props, propValue, node, elementName);
                    }

                    if (node.ChildNodes.Count > 0)
                    {
                        parent.AppendChild(node);
                    }

                    break;

                default:
                    break;
            }
        }
    }
}

問題 # 1: &プロパティのShouldSerializeValueメソッドが常に false を返す。EnabledVisible

問題 2:ShouldSerializeValueメソッド チェックをスキップしてもGetValuePropertyDescriptor常に return のメソッドをチェックしますTrue

現在の回避策: 回避策 として、現在Enabled&Visibleプロパティを を使用して非表示にし、BrowsableAttribute他の 2 つのブール型プロパティを作成し、 を使用しDisplayNameAttributeて表示名をEnable&に変更しましVisibleた。

しかし、この回避策では、これらのスニペットをすべてのコントロールに書き込む必要があります。

私は何かを見逃していますか、何か間違っていますか?Enabled&Visibleプロパティが変更されないのはなぜですか?

4

1 に答える 1

1

この問題についての長い議論はここにあります。(デッドリンク、新しいリンクが見つかりません)

このMSDN ページには、次のようなコメントがあります。

InheritedPropertyDescriptor クラスは、プロパティのデフォルト値を変更して、デフォルト値がオブジェクトのインスタンス化時の現在の値になるようにします。これは、プロパティが別のインスタンスから継承されているためです。デザイナーは、プロパティ値のリセットを、継承されたクラスによって設定された値に設定することとして定義します。この値は、メタデータに保存されているデフォルト値とは異なる場合があります。

ShouldSerializeValue の戻り値は、現在の値とデフォルト値の差に基づいているため、これはあなたの問題に直接関係していると思います。

これが、自分のコンテキストで何が起こるかを理解するのに役立つことを願っています.

于 2010-10-23T18:05:56.567 に答える