[英]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.