System.Windows.Forms.PropertyGrid - 如何在运行时根据条件隐藏项目 [英] System.Windows.Forms.PropertyGrid - How to hide Items based on conditions during runtime

查看:70
本文介绍了System.Windows.Forms.PropertyGrid - 如何在运行时根据条件隐藏项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要隐藏PropertyGrid中的Items,可以将Browsable选项设置为False,但是这不适用于PropertyGrid中的所有项目,我们需要它在运行时根据其他条件进行变量。 / p>

我们想知道是否有人知道a)这是否可行,第二个(如果是),b)是否有文档或示例集可用于此?


我们正在使用Visual Studio Professional 2015,Visual Basic,Winforms。



谢谢




一盎司的预防比一磅治疗更好。

解决方案

嗨约翰,


我认为您想要的是根据某些条件确定是否显示某些项目,我为此目的进行了演示,请参考以下代码:

 class StudentFactory 
{
public static Student CreateStudent(string sex)
{
if(sex ==" male")
{
return new MaleStudent();
}
else if(sex ==" female")
{
return new FemaleStudent();
}
其他
{
返回null;
}
}
}
class学生
{
公共学生()
{
}
字符串性别=" default" ;;
protected int age = 1;
protected int height = 2;
protected int weight = 3;
}
class MaleStudent:学生
{
public int年龄
{
获得{返回年龄; }
set {age = value; }
}
public int Height
{
get {return height; }
set {height = value; }
}
public int Weight
{
get {return weight; }
set {weight = value; }
}

}
class FemaleStudent:学生
{
[可浏览(假)]
public int年龄
{
获得{返回年龄; }
set {age = value; }
}
public int Height
{
get {return height; }
set {height = value; }
}
[可浏览(假)]
public int权重
{
得到{返回权重; }
set {weight = value; }
}
}

private void button1_Click(object sender,EventArgs e)
{
Student s = StudentFactory.CreateStudent(" male") ;
this.propertyGrid1.SelectedObject = s;
}

private void button2_Click(object sender,EventArgs e)
{
Student s1 = StudentFactory.CreateStudent(" female");
this.propertyGrid1.SelectedObject = s1;
}




希望这会有所帮助!


最好的问候,


Stanly


To hide Items from the PropertyGrid, it is possible to set the Browsable option to False, however this is not available for all Items in the PropertyGrid and we need it to be variable depending on other conditions during runtime.

We are wondering if anybody has an idea if a) this is possible at all and second (if it is), b) is there documentation or an example set available for this?

We are using Visual Studio Professional 2015, Visual Basic, Winforms.

Thanks


A ounce of prevention is better then a pound of cure.

解决方案

Hi John,

I think what you want is that according to some conditions to determine whether to display some items, I make a demo for this purpose and please refer to the following code:

        class StudentFactory
        {
            public static Student CreateStudent(string sex)
            {
                if (sex == "male")
                {
                    return new MaleStudent();
                }
                else if (sex == "female")
                {
                    return new FemaleStudent();
                }
                else
                {
                    return null;
                }
            }
        }
        class Student
        {
            public Student()
            {
            }
            string sex = "default";
            protected int age = 1;
            protected int height = 2;
            protected int weight = 3;
        }
        class MaleStudent : Student
        {
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
            public int Height
            {
                get { return height; }
                set { height = value; }
            }
            public int Weight
            {
                get { return weight; }
                set { weight = value; }
            }

        }
        class FemaleStudent : Student
        {
            [Browsable(false)]
            public int Age
            {
                get { return age; }
                set { age = value; }
            }
            public int Height
            {
                get { return height; }
                set { height = value; }
            }
            [Browsable(false)]
            public int Weight
            {
                get { return weight; }
                set { weight = value; }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Student s = StudentFactory.CreateStudent("male");
            this.propertyGrid1.SelectedObject = s;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Student s1 = StudentFactory.CreateStudent("female");
            this.propertyGrid1.SelectedObject = s1;
        }


Hope this helps!

Best Regards,

Stanly


这篇关于System.Windows.Forms.PropertyGrid - 如何在运行时根据条件隐藏项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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