如何在propertyGrid中显示一些属性 [英] how to show some properties in propertyGrid

查看:114
本文介绍了如何在propertyGrid中显示一些属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


i在运行时为我的标签添加了propertyGrid,我只想在propertyGrid中显示Text,ForeColor,Font

我该怎么办?

Hi i added propertyGrid in runtime for my label and i want just show "Text","ForeColor","Font" in propertyGrid
how can i do it?

推荐答案

MSDN [ ^ ]:

默认情况下,SelectedObject的所有公共属性都将显示在PropertyGrid中。您可以隐藏属性,使其不会显示在PropertyGrid控件中,方法是使用BrowsableAttribute进行装饰并将值设置为false。

All public properties of the SelectedObject will be displayed in the PropertyGrid by default. You can hide a property so that it is not displayed in the PropertyGrid control by decorating it with the BrowsableAttribute and setting the value to false.



所以有问题,您使用的是由他人控制(在这种情况下是微软),你无法控制什么属性是公开的,什么不是......

你有两种选择:

1.使用反思并使用 BrowsableAttribute = false修饰要隐藏的所有属性 [ ^ ](脏工作)

2.将原始控件包装在另一个只公开所请求属性的对象中...将该对象分配给PropertyGrid并使用包装原创在其他地方...


So there is your problem, you are using a control made by others (Microsoft in this case) and you can not control what property is public and what not...
You have two options:
1. Use refection and decorate all the properties you want to hide with BrowsableAttribute=false[^] (dirty job)
2. Wrap the original control inside an other that will make public only the requested properties...Assign that object to PropertyGrid and use the wrapped original in anywhere else...


有一个解决方法s不需要反射和修改PropertyGrid的古怪。试试这个:



1.创建一个UserControl,在其中放一个名为'button1的Button,或者其他什么。



2.根据需要设置UserControl的AutoSize,SizeMode,Padding,Properties等。



3.设置Button的AutoSize和SizeMode属性要求。在UserControl中按钮的大小和位置在你想要的位置。



...想想这里可能的交互,例如,在运行时更改FontSize :如果UserControl和Button都将其AutoSizeMode属性设置为'GrowAndShrink,并将其AutoSize模式设置为'Auto:那么Button和UserControl的大小将会改变...



4.编译,并根据需要将'ButtonExLimited实例拖放到您的表单上



5.示例:当您想要编辑暴露时属性网格中的属性:



propertyGrid1.SelectedObject = testButtonPropsExpose1.TheExposed;



6.示例:当您向按钮添加事件处理程序时:



testButtonPropsExpose1.TheExposed.TheControl.Click + = TheControl_Click;



7. UserControl的代码:
There is a work-around for this that does not require reflection and munging the weirdness of the PropertyGrid. Try this:

1. create a UserControl, put a Button in it named 'button1, or whatever.

2. set the UserControl's AutoSize, SizeMode, Padding, Properties, etc., as you require.

3. set the Button's AutoSize and SizeMode properties as you require. Size and position the Button in the UserControl where you want it.

... think about the possible interactions here of changing, for example, the FontSize at run-time: if both the UserControl and the Button have their AutoSizeMode Properties set to 'GrowAndShrink, and their AutoSize Mode set to 'Auto: then the Size of both Button and UserControl will change ...

4. compile, and drag-drop instances of 'ButtonExLimited onto your Form as you require

5. example: when you want to edit the exposed Properties in a Property Grid:

propertyGrid1.SelectedObject = testButtonPropsExpose1.TheExposed;

6. example: when you to add an Event Handler to Button:

testButtonPropsExpose1.TheExposed.TheControl.Click += TheControl_Click;

7. Code for the UserControl:
using System.Drawing;
using System.Windows.Forms;

namespace YourNameSpace
{
    public partial class TestButtonPropsExpose : UserControl
    {
        public TestButtonPropsExpose()
        {
            InitializeComponent();
            TheExposed.TheControl = this.button1;
        }

        public TheExposedProps TheExposed = new TheExposedProps();
    }

    public class TheExposedProps
    {
        public Control TheControl;

        public string Text
        {
            get { return TheControl.Text; }
            set { TheControl.Text = value; }
        }
        public Color ForeColor
        {
            get { return TheControl.ForeColor; }
            set { TheControl.ForeColor = value; }
        }

        public new Font Font
        {
            get { return TheControl.Font; }
            set { TheControl.Font = value; }
        }
    }
}

注意:



1.在生产代码中我使用的是一位发烧友这个版本我打算在CP上发布

Notes:

1. in production code I use a fancier version of this I intend to publish on CP


这篇关于如何在propertyGrid中显示一些属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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