简体   繁体   English

在PropertyGrid中隐藏属性而不影响Visual Studio Live属性

[英]Hide property in PropertyGrid without affecting Visual Studio Live Property

When applying the attribute [Browsable(false)] to a property, the PropertyGrid of wpftookit does not list this property any more (which is my goal). 将属性[Browsable(false)]应用于属性时,wpftookit的PropertyGrid不再列出该属性(这是我的目标)。

However, the Visual Studio tool "Live Property Editor" also stops displaying this property. 但是,Visual Studio工具“实时属性编辑器”也停止显示此属性。 This is not my goal and decreases debugging capabilities. 这不是我的目标,并且会降低调试功能。

I could of course work with compiler conditions like: 我当然可以处理编译器条件,例如:

#if DEBUG
[Browsable(false)]
#endif DEBUG

But this makes the property reappear in the PropertyGrid while working with a debug build (which I mostly do during development). 但这会使该属性在使用调试版本时重新出现在PropertyGrid中(我在开发过程中主要这样做)。

Is there a way of keeping the property visible in "Live Property Editor" but removing it from PropertyGrid? 有没有一种方法可以使该属性在“实时属性编辑器”中可见,但是将其从PropertyGrid中删除呢?

You could do this at runtime using TypeDescriptors and TypeConvertors by working out whether you are in Visual Studio or running as your program. 您可以在运行时使用TypeDescriptors和TypeConvertors做到这一点,方法是确定自己是在Visual Studio中还是作为程序运行。

There is another stack overflow answer: C# PropertyGrid => How to change visible Properties at Runtime which can give some pointers. 还有另一个堆栈溢出答案: C#PropertyGrid =>如何在运行时更改可见属性,属性可以提供一些指针。

There is no simple to use built-in support in PropertyGrid for dynamically altering which properties are visible depending on the value of another property. 使用PropertyGrid中的内置支持来简单地根据另一个属性的值动态更改哪些属性是可见的并不简单。 This doesn't mean it can't be done, just that it takes a bit of work. 这并不意味着它不能完成,只是需要一些工作。

As you've already discovered, what controls whether a property is visible or not is the BrowsableAttribute. 正如您已经发现的,控制属性是否可见的是BrowsableAttribute。 So basically you need to change this attribute dynamically, and the way to do that is to create your own TypeProvider and TypeDescriptor for your class, that dynamically returns the Browsable(false) or Browsable(true) attribute for the property to be hidden/shown depending on the value of another property in the class. 因此,基本上,您需要动态更改此属性,而实现此目的的方法是为您的类创建自己的TypeProvider和TypeDescriptor,该类将动态返回Browsable(false)或Browsable(true)属性以隐藏/显示该属性取决于类中另一个属性的值。 I will not attempt to describe how TypeProvider and TypeDescriptor works here, since it is quite a lengthy subject and there is much information readily available on this subject on the web already. 我不会尝试在这里描述TypeProvider和TypeDescriptor的工作方式,因为这是一个冗长的主题,并且在网络上已经有很多关于此主题的信息。

In addition you need to specify the [RefreshProperties(RefreshProperties.All)] attribute on the property controlling whether another property should be visible or not. 另外,您需要在属性上指定[RefreshProperties(RefreshProperties.All)]属性,以控制另一个属性是否可见。 This will force the propertygrid to requery the TypeDescriptor for the list of properties whenever its value is changed, giving your TypeDescriptor the chance to return a different set of properties, or different attributes on the properties it returns. 每当值更改时,这将迫使propertygrid重新查询TypeDescriptor以获取属性列表,这使您的TypeDescriptor有机会返回一组不同的属性,或者返回的属性具有不同的属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM