如何在 PropertyGrid 上自定义类别排序?
如何自定义 PropertyGrid
中的类别排序?
如果我设置以下任一选项...
propertyGrid.PropertySort = PropertySort.Categorized;
propertyGrid.PropertySort = PropertySort.CategorizedAlphabetical;
...那么类别将按字母顺序排列。 (“按字母顺序”似乎适用于每个类别中的属性。)如果我使用 PropertySort.NoSort
,我就会丢失分类。
我用 SelectObject
填充我的 PropertyGrid
,这非常简单:
this.propertyGrid1.SelectedObject = options;
options
是具有适当修饰属性的类的实例:
[CategoryAttribute("Category Title"),
DisplayName("Property Name"),
Browsable(true),
ReadOnly(false),
BindableAttribute(true),
DesignOnly(false),
DescriptionAttribute("...")]
public bool PropertyName {
get {
// ...
}
set {
// ...
this.OnPropertyChanged("PropertyName");
}
}
我有一个六个类别中的几十个属性。
有什么方法可以调整类别排序顺序,同时保持 SelectedObject
的易用性?
How can I customize the sorting of categories in a PropertyGrid
?
If I set either of the following...
propertyGrid.PropertySort = PropertySort.Categorized;
propertyGrid.PropertySort = PropertySort.CategorizedAlphabetical;
... then the categories will be alphabetized. ("Alphabetical" would seem to apply to the properties within each category.) If I use PropertySort.NoSort
, I lose categorization.
I'm populating my PropertyGrid
with SelectObject
, which is pretty easy:
this.propertyGrid1.SelectedObject = options;
options
is an instance of a class with suitably decorated properties:
[CategoryAttribute("Category Title"),
DisplayName("Property Name"),
Browsable(true),
ReadOnly(false),
BindableAttribute(true),
DesignOnly(false),
DescriptionAttribute("...")]
public bool PropertyName {
get {
// ...
}
set {
// ...
this.OnPropertyChanged("PropertyName");
}
}
I have a few dozen properties in half a dozen categories.
Is there some way I can adjust the category sort order while preserving my ease of use with SelectedObject
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
上面描述的“\t”技巧的一个小变化,我只是尝试用回车符(“\r”)代替。 它似乎有效并避免了由选项卡引入的额外空间引起的工具提示问题。
A small variation on the '\t' trick described above, I just tried it with carriage return characters ('\r') instead. It seems to work and avoids the tooltip problem caused by the extra space introduced by a tab.
所有其他答案都解决了如何自定义排序顺序,但没有解决用户单击分类或按字母顺序按钮时出现的问题。
单击这些按钮会将
CategorizedAlphabetical
或Alphabetical
值分配给PropertySort
属性,而通常(至少对我来说)所需的行为是他们分配分类
或字母
值。通过添加事件
PropertySortChanged
可以获得正确的行为:通过使用此事件,我看不到工具提示的问题,因为我只放置了
\t
在类别名称前面,而不是在属性名称前面。All the other answers address how to customize the sorting order, but don't address the problem that arises when the user clicks on the Categorized or Alphabetical buttons.
Clicking on those buttons assigns the
CategorizedAlphabetical
orAlphabetical
values to thePropertySort
property, while usually (at least for me) the desired behavior would be for them to assign theCategorized
orAlphabetical
values.By adding the event
PropertySortChanged
it is possible to get the correct behavior:By using this event I don't see the problem with the tooltip, because I put the
\t
only in front of the category names, not in front of the property names.如果您的意思是希望以特定(非字母顺序)方式对类别进行排序,那么不行 - 我认为您不能这样做。 您可能想尝试 VisualHint - 我希望它确实有这个(因为您可以获得更多控制权)。
If you mean that you want the categories sorted in a specific (non-alphabetical) way, then no - I don't think you can do that. You might want to try VisualHint - I expect it does have this (since you can seize a lot more control).
就像 @Marc Gravel 在他的答案中所说,框架中没有任何内容允许这种行为。 任何解决方案都将是一个黑客。 话虽如此,您可以使用 @Shahab 在 他的答案 中建议的解决方案作为解决方法,但这并不向维护您的代码的任何人真正表明您的意图。 所以我认为你能做的最好的事情就是创建一个自定义的
Attribute
,它继承自CategoryAttribute
来为你处理这个过程:然后你就可以这样使用它
只需确保你设置将
PropertyGrid
的UseCompatibletextRendering
属性设置为true
,以便为您和PropertySort
集去除不可打印的字符到Categorized
或CategorizedAlphabetical
,你应该可以开始了。Like @Marc Gravel said in his answer, there's nothing in the framework that allows this behaviour. Any solution will be a hack. With that said, you can use the solution suggested by @Shahab in his answer as a work-around but that doesn't really indicate your intention to anyone maintaining your code. So I think the best you can do is create a custom
Attribute
which inherits fromCategoryAttribute
to handle the process for you:Then you can use it as such
Just make sure you set the
PropertyGrid
'sUseCompatibletextRendering
property totrue
to strip out the non-printable characters for you and thePropertySort
set toCategorized
orCategorizedAlphabetical
and you should be good to go.我认为这个链接很有用
http://bytes.com/groups/ net-c/214456-q-ordering-sorting-category-text-propertygrid
I think this link is useful
http://bytes.com/groups/net-c/214456-q-ordering-sorting-category-text-propertygrid