在运行时更改可浏览属性(C#) [英] Changing Browsable Attribute at Runtime (C#)

查看:70
本文介绍了在运行时更改可浏览属性(C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行时更改我的一个类中的变量的Browsable属性.包含属性的类如下所示:

I'm trying to change the Browsable attribute of a variable in one of my classes at runtime. The class containing the attribute looks like this:

public class DisplayWallType : WallType
    {
        [TypeConverter(typeof(SheathingOptionsConverter))]
        [Browsable(false)]
        public override Sheathing SheathingType { get; set; }

        public DisplayWallType(string passedName, string passedType, bool passedMirrorable, Distance passedHeight, string passedStudPattern, Distance passedStudSpacing, VaporBarrier passedBarrier)
            : base(passedName, passedType, passedMirrorable, passedHeight, passedStudPattern, passedStudSpacing, passedBarrier)
        {

        }

        /// <summary>
        /// Empty constructor for XML serialization
        /// </summary>
        public DisplayWallType()
        {

        }
    }

我最初将SheathingType设置为false,因为我不希望该属性出现在应用程序的第一种形式中.但是,我确实希望它在第二种形式中可见,所以我有此方法可以对其进行更改

I initially set it to false for SheathingType because I don't want that attribute to show up in the first form of my application. However, I do want it visible in my second form so I have this method to change it

private void _makeSheathingVisible(DisplayWallType wallTypeToMakeSheathingVisible)
        {
            PropertyDescriptor descriptor = TypeDescriptor.GetProperties(wallTypeToMakeSheathingVisible.GetType())["SheathingType"];
            BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
            FieldInfo isBrow = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
            isBrow.SetValue(attrib, true);
        }

上面的方法采用DisplayWallType对象,并确保将Browsable设置为true.我的第二种形式是结合了属性网格的树状视图.树形视图中填充DisplayWallType的实例和当选择一个,它被传递到该方法使得SheathingType示出了与从父类的属性的其余部分PropertiesGrid.

That method above takes a DisplayWallType object and ensures that Browsable is set to true. My second form is a treeview combined with a property grid. The treeview is populated with instances of DisplayWallType and when one is selected, it is passed into that method so that SheathingType shows up in the PropertiesGrid with the rest of the properties from the parent class.

但是,SheathingType仍然没有显示为属性,因此我的方法有问题,但是我不知道是什么

However, SheathingType still does not show up as a property so something is wrong with my approach but I have no idea what

推荐答案

我知道这个问题是从前一段时间开始的,但是今天我通过更改Browsable属性后调用propertiesGrid.Refresh()来解决了同一问题.也许将来对某人有帮助.

I know this issue is from some time ago, but I fixed the same issue today by calling propertiesGrid.Refresh() after changing the Browsable attribute. Maybe it helps someone in the future.

这篇关于在运行时更改可浏览属性(C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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