简体   繁体   English

如何在Visual Studio扩展的“属性窗口对象列表”中修改名称

[英]How to modify name in Properties Window Object List for Visual Studio extension

I am writing an extension for Visual Studio and have created a class that I use to display custom information in the Properties Window. 我正在为Visual Studio编写扩展,并创建了一个类,用于在“属性”窗口中显示自定义信息。 I would like to modify the text that is shown in the object list at the top of the properties window, but have been unable to find a way to do this. 我想修改属性窗口顶部的对象列表中显示的文本,但是无法找到执行此操作的方法。 I found this page which seems to describe what I want: 我发现这个页面似乎描述了我想要的东西:

Properties Window Object List 属性窗口对象列表

However, that description doesn't seem to work. 但是,该描述似乎不起作用。 First of all, the description states that the "object name displayed to the left of the object type in bold is retrieved from the object itself using the Name property provided by the IProvideClassInfo interface", but IProvideClassInfo doesn't have a property named "Name". 首先,描述声明“使用IProvideClassInfo接口提供的Name属性从对象本身检索以粗体显示在对象类型左侧的对象名称”,但IProvideClassInfo没有名为“Name”的属性”。 Also the description states that the method "GetClassInfo" of the class "IProvideClassInfo" returns an "ITypeInfo", but that function has an output parameter of type "Type", not "ITypeInfo". 该描述还指出类“IProvideClassInfo”的方法“GetClassInfo”返回“ITypeInfo”,但该函数具有类型“Type”的输出参数,而不是“ITypeInfo”。

The class for which I want to display information in the properties window currently looks something like this: 我想在属性窗口中显示信息的类当前看起来像这样:

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

The property "MyProperty" shows up nicely with the correct description and category, but I have not been successful in modifying the text in the object list. 属性“MyProperty”很好地显示了正确的描述和类别,但我还没有成功修改对象列表中的文本。 I have tried to make the class "MyClass" extend the interface "IProvideClassInfo", but the method "GetClassInfo" doesn't seem to be executed when the information is displayed in the properties window. 我试图让类“MyClass”扩展接口“IProvideClassInfo”,但是当信息显示在属性窗口中时,似乎没有执行方法“GetClassInfo”。

What am I missing here? 我在这里错过了什么?

I asked this question in a chat and the answer is that you need to implement ICustomTypeDescriptor interface or derive from the class CustomTypeDescriptor . 我在聊天中问了这个问题,答案是你需要实现ICustomTypeDescriptor接口或者从类CustomTypeDescriptor派生

The respective methods you need to implement are GetComponentName (the bold/first name in the gridview-header) and GetClassName (the light/second name in the gridview-header). 您需要实现的相应方法是GetComponentName(gridview-header中的粗体/名字)和GetClassName(gridview-header中的light / second名称)。

However, if you just implement these two methods, no other properties will be displayed in the property-grid. 但是,如果只实现这两种方法,则属性网格中不会显示其他属性。 AnkhSVN solved this in their AnkhPropertyGridItem (big thanks to rhuijben for this solution): 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;
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 Visual Studio 扩展显示弹出 window(不是工具窗口)? - How to display popup window (not tool window) from Visual Studio extension? 在WPF / C#中为多个对象创建Visual Studio属性窗口 - Creating a Visual Studio properties window for multiple object in WPF/C# 列出对象属性,例如“ Visual Studio立即”窗口 - Listing object properties like the Visual Studio Immediate window 如何在Visual Studio中修改新WinForm的默认属性 - How to modify default properties of new WinForm in visual studio 如何以分层方式保存 Visual Studio 项目扩展属性 - How to save Visual Studio project extension properties in a tiered way 将类属性公开给Visual Studio属性窗口 - Expose Class properties to Visual Studio Properties window 如何以编程方式打开Visual Studio扩展的工具窗口? - How to open a tool window of a visual studio extension programmatically? 如何创建将REPL窗口添加到Visual Studio的扩展 - How to create extension which adds REPL window to Visual Studio 如何显示Visual Studio扩展中的“添加连接”窗口? - How to show the window “Add connection” from the extension for Visual Studio? Visual Studio扩展开发:如何参考未决更改窗口 - Visual Studio Extension Development: How reference pending changes window
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM