【问题标题】:How to modify name in Properties Window Object List for Visual Studio extension如何在 Visual Studio 扩展的属性窗口对象列表中修改名称
【发布时间】:2015-10-19 08:40:55
【问题描述】:

我正在为 Visual Studio 编写一个扩展,并创建了一个用于在“属性”窗口中显示自定义信息的类。我想修改显示在属性窗口顶部的对象列表中的文本,但一直无法找到执行此操作的方法。我发现这个页面似乎描述了我想要的:

Properties Window Object List

但是,该描述似乎不起作用。首先,描述中指出“对象类型左侧以粗体显示的对象名称是使用 IProvideClassInfo 接口提供的 Name 属性从对象本身检索的”,但 IProvideClassInfo 没有名为“Name ”。描述还指出,“IProvideClassInfo”类的“GetClassInfo”方法返回“ITypeInfo”,但该函数的输出参数类型为“Type”,而不是“ITypeInfo”。

我想在属性窗口中显示信息的类目前看起来像这样:

public class MyProperties
{
    [Description("MyDescription")]
    [Category("MyCategory")]
    public string MyProperty { get { return "The text"; } }
}

属性“MyProperty”很好地显示了正确的描述和类别,但我没有成功修改对象列表中的文本。我试图让“MyClass”类扩展接口“IProvideClassInfo”,但是在属性窗口中显示信息时似乎没有执行“GetClassInfo”方法。

我在这里错过了什么?

【问题讨论】:

    标签: c# visual-studio plugins


    【解决方案1】:

    我在chat 中问过这个问题,答案是你需要实现ICustomTypeDescriptor 接口或者派生自CustomTypeDescriptor 类。

    您需要实现的相应方法是 GetComponentName(gridview-header 中的粗体/名字)和 GetClassName(gridview-header 中的浅色/第二个名称)。

    但是,如果只实现这两个方法,则属性网格中不会显示其他属性。 AnkhSVN 在他们的AnkhPropertyGridItem 中解决了这个问题(非常感谢rhuijben 的解决方案):

    // Copyright 2008 The AnkhSVN Project
    //
    //  Licensed under the Apache License, Version 2.0 (the "License");
    //  you may not use this file except in compliance with the License.
    //  You may obtain a copy of the License at
    //
    //    http://www.apache.org/licenses/LICENSE-2.0
    //
    //  Unless required by applicable law or agreed to in writing, software
    //  distributed under the License is distributed on an "AS IS" BASIS,
    //  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    //  See the License for the specific language governing permissions and
    //  limitations under the License.
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;
    
    namespace Ankh.Scc
    {
        /// <summary>
        /// Base class for classes that are designed to be shown in the VS Property grid
        /// </summary>
        public abstract class AnkhPropertyGridItem : CustomTypeDescriptor
        {
            /// <summary>
            /// Gets the light/second name shown in the gridview header
            /// </summary>
            [Browsable(false)]
            protected abstract string ClassName
            {
                get;
            }
    
            /// <summary>
            /// Gets the bold/first name shown in the gridview header
            /// </summary>
            [Browsable(false)]
            protected abstract string ComponentName
            {
                get;
            }
    
    
            /// <summary>
            /// Returns the name of the class represented by this type descriptor.
            /// </summary>
            /// <returns>
            /// A <see cref="T:System.String"/> containing the name of the component instance this type descriptor is describing. The default is null.
            /// </returns>
            public override sealed string GetComponentName()
            {
                return ComponentName;
            }
    
            /// <summary>
            /// Returns the fully qualified name of the class represented by this type descriptor.
            /// </summary>
            /// <returns>
            /// A <see cref="T:System.String"/> containing the fully qualified class name of the type this type descriptor is describing. The default is null.
            /// </returns>
            public override sealed string GetClassName()
            {
                return ClassName;
            }
    
            TypeConverter _rawDescriptor;
            TypeConverter Raw
            {
                get { return _rawDescriptor ?? (_rawDescriptor = TypeDescriptor.GetConverter(this, true)); }
            }
    
            /// <summary>
            /// Returns a collection of property descriptors for the object represented by this type descriptor.
            /// </summary>
            /// <returns>
            /// A <see cref="T:System.ComponentModel.PropertyDescriptorCollection"/> containing the property descriptions for the object represented by this type descriptor. The default is <see cref="F:System.ComponentModel.PropertyDescriptorCollection.Empty"/>.
            /// </returns>
            public override PropertyDescriptorCollection GetProperties()
            {
                return Raw.GetProperties(this);
            }
    
            /// <summary>
            /// Returns a type converter for the type represented by this type descriptor.
            /// </summary>
            /// <returns>
            /// A <see cref="T:System.ComponentModel.TypeConverter"/> for the type represented by this type descriptor. The default is a newly created <see cref="T:System.ComponentModel.TypeConverter"/>.
            /// </returns>
            public override TypeConverter GetConverter()
            {
                return Raw;
            }
    
            /// <summary>
            /// Returns a filtered collection of property descriptors for the object represented by this type descriptor.
            /// </summary>
            /// <param name="attributes">An array of attributes to use as a filter. This can be null.</param>
            /// <returns>
            /// A <see cref="T:System.ComponentModel.PropertyDescriptorCollection"/> containing the property descriptions for the object represented by this type descriptor. The default is <see cref="F:System.ComponentModel.PropertyDescriptorCollection.Empty"/>.
            /// </returns>
            public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
            {
                return Raw.GetProperties(null, null, attributes);
            }
    
            /// <summary>
            /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
            /// </summary>
            /// <returns>
            /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
            /// </returns>
            public override string ToString()
            {
                return ClassName;
            }
    
            /// <summary>
            /// Returns an object that contains the property described by the specified property descriptor.
            /// </summary>
            /// <param name="pd">The property descriptor for which to retrieve the owning object.</param>
            /// <returns>
            /// An <see cref="T:System.Object"/> that owns the given property specified by the type descriptor. The default is null.
            /// </returns>
            public override object GetPropertyOwner(PropertyDescriptor pd)
            {
                return this;
            }
        }
    }
    

    【讨论】:

    • 感谢您撰写此答案。这正是我想我应该添加的参考:)
    • 我听从了你从 CustomTypeDescriptor 派生的建议,它就像一个魅力!谢谢!你让我开心:-)
    猜你喜欢
    • 1970-01-01
    • 2013-03-04
    • 1970-01-01
    • 2011-08-31
    • 1970-01-01
    • 2022-11-03
    • 2017-12-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多