在System.Windows.Forms.PropertyGrid控制使用自定义PropertyTab的 [英] Using Custom PropertyTab in System.Windows.Forms.PropertyGrid Control

查看:329
本文介绍了在System.Windows.Forms.PropertyGrid控制使用自定义PropertyTab的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用的WinForms PropertyGrid控件的自定义属性选项卡的功能,但它有我殴打。我特别想在运行时PropertyGrid中用来显示对象属性,而不是试图创造一个控制设计时支持,尽管他们似乎是类似技术的应用。

I am trying to use the customisable property tab feature of the WinForms PropertyGrid control but it has me beaten. I am specifically trying to use in runtime a PropertyGrid to display object properties, as opposed to trying to create design-time support for a control, though they seem to be applications of similar technology.

这个问题我已经严重混淆。我试图效仿发表了几篇教程,并没有得出任何实际固体的结论。

This question has me seriously confused. I have attempted to follow several tutorials on the topic and none come to any real solid conclusion.

文献综述

http://msdn.microsoft.com/en-us/库/ aa302334.aspx#vsnetpropbrow_topic05

这篇文章告诉读者,添加属性选项卡是很容易可行,而且code样品中包括FunkyButton项目,即,不包括任何地方。

This article informs the reader that adding property tabs is easily doable and that the code sample is in the included FunkyButton project, that, isn't included anywhere.

HTTP://www.$c$cproject.com/KB/网格/ PropertyGridExWinForms.aspx

该项目建议使用自定义属性选项卡中的唯一方法是通过覆盖比如GetService的()私有方法来扩展基本属性网格功能。我敢肯定这不是必须的,并试图利用其信息来创建一个服务提供商。这个类在属性网格的SelectedObject的需要class属性PropertyTabAttribute被分配给一个扩展System.Windows.Forms.PropertyGridInternal.PropertiesTab,我已经创建并使用它没有然而赐教。

This project suggests the only way to use custom property tabs is to extend the base property grid functionality by overriding for instance the GetService() private method. I am quite sure this is not required, and attempted to use its information to create a service provider. It did however enlighten me that the class of the SelectedObject in the property grid needs the class attribute PropertyTabAttribute to be assigned to an extended System.Windows.Forms.PropertyGridInternal.PropertiesTab, which I have created and used.

http://www.dacris.com/blog/2008/01/02/net-how-to-show-events-tab-in-a-property-grid/

此文章表明属性网格本身需要使用PropertyGrid.PropertyTabs.AddTabType()方法被添加的标签。然后,它揭示了需要的属性网格的站点财产分配,然后得意地暗示我应该已经知道如何做到这一点:...你需要给电网的网站属性设置为一些你所设计的网站实现。如果你托管自己的设计师,你应该知道如何检索这些。

This article indicates that the property grid itself requires the tab to be added using the PropertyGrid.PropertyTabs.AddTabType() method. It then reveals that the Site property of the property grid needs to be assigned and then smugly suggests I should already know how to do this: "...you need to set the Site property of the grid to some designer site that you’ve implemented. If you’re hosting your own designers, you should know how to retrieve this."

http://www.syncfusion.com/FAQ/windowsforms/faq_c83c。 ASPX#q913q

在开始写这个问题,我发现了一个链接,这短短的文章上包含的code的话题。该项目更侧重似乎在设计时功能但是当我用自己在一个完全香草的PropertyGrid对象的实例,它显示了这两个属性选项卡按钮,它们的自定义标签的对象。我可以毫不费力地相关视图之间切换。但该项目似乎表明,现在的自定义属性需要区分可用视图之间的性能。这篇文章现在建议我忽略了网站的定义,专业PropertyGrids和服务供应商,集中显示的对象本身。

In starting to write this question I found a link to this short article on the topic with included code. This project is more focused it seems on design-time functionality however when I used an instance of their object in a completely vanilla PropertyGrid, it showed up both the Properties tab button and their Custom Tab object. I was able to switch between the associated views effortlessly. However the project seems to suggest now custom attributes are required to distinguish properties between the available views. This article now suggests I ignore the Site definitions, specialised PropertyGrids and service providers, and concentrate on the displayed object itself.

无奈的是,在修改这个项目更接近反映了我的申请,我感动的业务属性网格到一个选项卡控件的标签页,其中,它打破了以显示默认属性标签的能力属性网格,因此这意味着我可以切换到自定义属性选项卡,但从来没有回来。

Frustratingly, in modifying this project to closer reflect my application, I moved the operational property grid to a tab control's tab page, wherein it broke the ability for the property grid to show the default Properties tab, thus meaning I could switch to the custom property tab, but never back.

结论

我在一个完全丧失。我一定要近,但所有的途径我试图让这项工作,也许我的大脑只是炒。什么是关键,使属性选项卡的工作? 它是:

I am at a complete loss. I must be close, but of all the avenues I have tried to make this work, perhaps my brain is just fried. What is the key to making property tabs work? Is it:

  • 的PropertyGrid.SelectedObject的具体类的定义?
  • 设置属性网格本身?
  • 关于自定义选项卡定义?

难道仅仅说明的最后一个项目工作,因为SelectedObject从用户控件继承?是否SelectedObject需要从System.ComponentModel.Component继承?

Did the last project described only work because the SelectedObject inherited from UserControl? Does the SelectedObject need to inherit from System.ComponentModel.Component?

如果有人可以尝试清除所有这一切并提供了如何使它工作,我将不胜感激。最简洁的可能说明

If someone could try to clear this all up and provide the most concise possible description of how to make it work, I would be most grateful.

非常感谢。

推荐答案

下面是一个自定义属性选项卡的code:

Here is the code of a custom property tab:

public class CustomTab : PropertyTab
{
    private Bitmap _bitmap;

    // the tooltip displayed in the grid
    public override string TabName
    {
        get
        {
            return "CustomTab";
        }
    }

    // the icon displayed in the grid
    public override Bitmap Bitmap
    {
        get
        {
            if (_bitmap == null)
            {
                // 1. create a file named "CustomTab.bmp". It must be a 16x16, 8-bit bitmap. Transparency pixel is magenta.
                // 2. place it in the project aside this .cs
                // 3. configure its build action to "Embedded Resource"
                _bitmap = new Bitmap(GetType(), GetType().Name + ".bmp");
            }
            return _bitmap;
        }
    }

    // TODO: return the descriptor corresponding to the properties you want to show
    public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object component, Attribute[] attributes)
    {
        return new PropertyDescriptorCollection(null);
    }

    public override PropertyDescriptorCollection GetProperties(object component, Attribute[] attributes)
    {
        return GetProperties(null, component, attributes);
    }
}

这里是如何将其添加到属性网格:

And here is how you can add it to the property grid:

CustomTab tab = new CustomTab();
grid.PropertyTabs.AddTabType(typeof(CustomTab), PropertyTabScope.Global);

它会显示什么,因为我不回,在超负荷的GetProperties任何的PropertyDescriptor,但是这就是你需要定义你想使用的描述符。

It will display nothing because I don't return any PropertyDescriptor in the GetProperties overload, but this is where you need to defined what descriptor you want to use.

OP编辑:

看来,使属性选项卡的工作,清理了信息的混乱可在网络上的症结所在:

It appears that the crux of making property tabs work, clearing up the confusion of the information available on the web:

  • 显示的对象不需要继承像控件或组件的任何

  • The displayed object need not inherit anything like Control or Component

在PropertyGrid.Site属性不需要设置

The PropertyGrid.Site property need not be set

属性网格不需要扩展自定义类

The property grid need not be extended with a custom class

属性网格甚至不必明确添加的标签,如果它被称为出在SelectedObject由一个属性。当然持久标签需要被添加

The property grid need not even explicitly add the tab if it is called out in the SelectedObject by an attribute. Of course a persistent tab would need to be added.

确保所有三个PropertiesTab.GetProperties方法被覆盖,并指向所需的方法,而不是基方法。

Ensure all three PropertiesTab.GetProperties methods are overridden and point to the required method and not the base method.

该标签的位图覆盖必须做一些事情!

The tab's bitmap override must actually do something!

这篇关于在System.Windows.Forms.PropertyGrid控制使用自定义PropertyTab的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆