【PropertyGrid问题】如何禁止展开 Font,Image,Size 等类型的属性

blackfire1024 2011-03-09 01:46:02
使用 PropertyGrid 显示和修改控件的属性的确很方便

但随之而来的问题是有些控件的属性,
我不希望PropertyGrid显示展开的子属性(嵌套属性)
通俗点讲就是不希望它显示前面的加号

比如
Control的Size
TextBox的Font
PictureBox的Image

这些属性都会额外的展开其子属性
Size:Width,Height;
Font:Name,Size,Unit...;
Image:RawFormat,Size...;
...

目的也很简单,
就是为了多语言显示,
当这些结构为控件属性时,
无法控制其结构属性显示的文本,
因此希望禁止显示这些结构属性


网上的例子也查了很多
实现 ICustomTypeDescriptor
创建自定义的 PropertyDescriptor
同样只能处理第一层的属性,
而对于子属性依旧无法控制

当 PropertyGrid 要显示某一个对象的属性时
会同时检查该属性的Attributes
是否包含 TypeConverterAttribute
即该类型是否支持 TypeConverter

一旦检测到该类型存在自定义的 TypeConverter
那么就会调用其中的 GetProperties 和 GetPropertiesSupported 方法
获取该类型的属性信息(如 Size的Width和Height)

自定义的类型默认是不展开的,
因为它未实现对应的 TypeConverter

可惜的是象Size、Image、Font这些基础结构
是有对应的 TypeConverter 实现的
Size --> SizeConverter
Image --> ImageConverter
Font --> FontConverter

于是
假如当我有一个自定义类型 Custom
包含了属性 Prop ,Prop 是这些基础结构(Size、Image、Font)时
无论我怎么做,在PropertyGrid中都会把 Prop 属性展开
而开展的属性对于用户来讲是无任何意义的

希望有遇到此类问题并解决的朋友能指导一下
未遇到此类问题的朋友可以一起讨论研究一下

谢谢
...全文
379 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
blackfire1024 2011-03-09
  • 打赏
  • 举报
回复
刚又试了一下

通过实现 ICustomTypeDescriptor +
继承 PropertyDescriptor 的方法
还是可以达到我的目的

关键在于重写 PropertyDescriptor 中的 Converter 属性
判断当遇到这些不需要显示子属性的类型(如Size)时
返回自定义的 HideSubPropConverter

不过也有不方便之处是我要为每一个这样的类型
实现相应的 TypeConverter 和 PropertyDescriptor
bloodish 2011-03-09
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 blackfire1024 的回复:]
那通过继承 PropertyGrid 能不能达到目的呢?
[/Quote]
没有尝试过,不好判断.
个人觉得,如果你要实现工作量不小.
blackfire1024 2011-03-09
  • 打赏
  • 举报
回复
那通过继承 PropertyGrid 能不能达到目的呢?
bloodish 2011-03-09
  • 打赏
  • 举报
回复
如果要显示属性的控件是你自定义的控件,可以隐藏基类的属性,如:

[TypeConverter(typeof(HideSizeSubPropConverter))]
public new Size Size
{
get { return base.Size; }
set { base.Size = value; }
}

如果是标准控件,跟Property关联的Attribute/Converter是无法动态修改的
blackfire1024 2011-03-09
  • 打赏
  • 举报
回复
我不想重新定义一个 MySize的属性
去完成和 Size 属性一模一样的功能

除非没有其他方法了,才考虑这个办法
blackfire1024 2011-03-09
  • 打赏
  • 举报
回复
继承并实现 TypeConverter 是没问题啊
但是继承好的类 要用 TypeConverterAttribute 和 类或者对应属性关联

类似 Control.Size 我是无法修改他的 TypeConverterAttribute的
因为在 .net 内部已经关联好了
bloodish 2011-03-09
  • 打赏
  • 举报
回复
对于Size

public class HideSizeSubPropConverter : SizeConverter
{
public override PropertyDescriptorCollection GetProperties(ITypeDescriptorContext context, object value, Attribute[] attributes)
{
return new PropertyDescriptorCollection(null); ;
}
}
[TypeConverter(typeof(HideSizeSubPropConverter))]
public Size MySize { get; set; }

Font,Point类似处理即可
Version 13.0.2.13 (4/16/2021) TwwDataGrid - Performance with most recent OS X/BigSur was noticeably slower in painting/scrolling. We recommend adding the following line of code to your project source before the initialize which uses metal graphics and is faster to paint. ​begin FMX.Types.GlobalUseMetal := True; // add this line. Application.Initialize; Application.CreateForm(TMainDemoForm, MainDemoForm); end; ​ Note that also this build has a new method of painting the default text in the grid that is significantly faster even when metal is not used. If there are any unforeseen issues, you can return to the old behavior by adding this line of code in your main project source. ​ wwGlobalManager.InfoPowerControlsManager.TextPaintingNew:= false; Application.Initialize; ​ Note: You will need to add the units fmx.types to resolve the GlobalUseMetal reference and add fmx.wwmanager to resolve the TextPaintingNew reference. TwwDataGrid - Footer painting would incorrectly clear grid's data rows in some cases such as when the Imagecontrol property was assigned as the background image.. This problem is fixed. TwwLayoutGrid/TwwDataGrid - Retina painting on Mac OS X was not as sharp before for custom controls. TwwLayoutGrid - TwwEdit embedded in layout grid with a different font size (overridestylesettings.fontsize assigned) would paint text too large before in OSX. This problem is fixed. TwwListBox - Right Accessory icon would disappear if changing visibility of controls within the listbox during runtime. This problem is fixed.

111,120

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Creator Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧