如何修改Visual Studio扩展的属性窗口对象列表中的名称
本文关键字:窗口 属性 对象 列表 扩展 何修改 修改 Studio Visual | 更新日期: 2023-09-27 18:18:21
我正在为Visual Studio编写扩展,并创建了一个类,用于在属性窗口中显示自定义信息。我想修改在属性窗口顶部的对象列表中显示的文本,但一直无法找到这样做的方法。我找到了这个页面,它似乎描述了我想要的:
属性窗口对象列表
然而,这种描述似乎不起作用。首先,描述声明"在对象类型左侧以粗体显示的对象名称是使用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"似乎没有执行当信息显示在属性窗口。
我在这里错过了什么?
我在聊天中问过这个问题,答案是您需要实现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;
}
}
}