如何在 PropertyGrid 上自定义类别排序?

发布于 2024-07-19 13:13:52 字数 1005 浏览 7 评论 0原文

如何自定义 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

春夜浅 2024-07-26 13:13:57

上面描述的“\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.

盗梦空间 2024-07-26 13:13:57

所有其他答案都解决了如何自定义排序顺序,但没有解决用户单击分类按字母顺序按钮时出现的问题。

单击这些按钮会将 CategorizedAlphabeticalAlphabetical 值分配给 PropertySort 属性,而通常(至少对我来说)所需的行为是他们分配分类字母值。

通过添加事件 PropertySortChanged 可以获得正确的行为:

private void propertyGrid1_PropertySortChanged(object sender, EventArgs e)
{
    if (propertyGrid1.PropertySort == PropertySort.CategorizedAlphabetical)
        propertyGrid1.PropertySort = PropertySort.Categorized;
}

通过使用此事件,我看不到工具提示的问题,因为我只放置了 \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 or Alphabetical values to the PropertySort property, while usually (at least for me) the desired behavior would be for them to assign the Categorized or Alphabetical values.

By adding the event PropertySortChanged it is possible to get the correct behavior:

private void propertyGrid1_PropertySortChanged(object sender, EventArgs e)
{
    if (propertyGrid1.PropertySort == PropertySort.CategorizedAlphabetical)
        propertyGrid1.PropertySort = PropertySort.Categorized;
}

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.

我们只是彼此的过ke 2024-07-26 13:13:56

如果您的意思是希望以特定(非字母顺序)方式对类别进行排序,那么不行 - 我认为您不能这样做。 您可能想尝试 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).

我的影子我的梦 2024-07-26 13:13:55

就像 @Marc Gravel 在他的答案中所说,框架中没有任何内容允许这种行为。 任何解决方案都将是一个黑客。 话虽如此,您可以使用 @Shahab 在 他的答案 中建议的解决方案作为解决方法,但这并不向维护您的代码的任何人真正表明您的意图。 所以我认为你能做的最好的事情就是创建一个自定义的 Attribute ,它继承自 CategoryAttribute 来为你处理这个过程:

public class CustomSortedCategoryAttribute : CategoryAttribute
{
    private const char NonPrintableChar = '\t';

    public CustomSortedCategoryAttribute(   string category,
                                            ushort categoryPos,
                                            ushort totalCategories)
        : base(category.PadLeft(category.Length + (totalCategories - categoryPos),
                    CustomSortedCategoryAttribute.NonPrintableChar))
    {
    }
}

然后你就可以这样使用它

[CustomSortedCategory("Z Category",1,2)]
public string ZProperty {set;get;}
[CustomSortedCategory("A Category",2,2)]
public string AProperty {set;get;}

只需确保你设置将 PropertyGridUseCompatibletextRendering 属性设置为 true,以便为您和 PropertySort 集去除不可打印的字符到 CategorizedCategorizedAlphabetical ,你应该可以开始了。

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 from CategoryAttribute to handle the process for you:

public class CustomSortedCategoryAttribute : CategoryAttribute
{
    private const char NonPrintableChar = '\t';

    public CustomSortedCategoryAttribute(   string category,
                                            ushort categoryPos,
                                            ushort totalCategories)
        : base(category.PadLeft(category.Length + (totalCategories - categoryPos),
                    CustomSortedCategoryAttribute.NonPrintableChar))
    {
    }
}

Then you can use it as such

[CustomSortedCategory("Z Category",1,2)]
public string ZProperty {set;get;}
[CustomSortedCategory("A Category",2,2)]
public string AProperty {set;get;}

Just make sure you set the PropertyGrid's UseCompatibletextRendering property to true to strip out the non-printable characters for you and the PropertySort set to Categorized or CategorizedAlphabetical and you should be good to go.

丢了幸福的猪 2024-07-26 13:13:54

我认为这个链接很有用
http://bytes.com/groups/ net-c/21​​4456-q-ordering-sorting-category-text-propertygrid

我不相信有办法做到这一点。 我唯一能做的就是
发现表明您可能能够执行此操作的是 PropertySort
财产。 如果设置为none,则表示显示属性
按照从类型描述符接收它们的顺序。 你可能是
能够在您的对象和对象之间创建代理类型描述符
propertygrid,它不仅会返回正确的属性
顺序,但属性的类别按您想要的顺序排列
他们在...

I think this link is useful
http://bytes.com/groups/net-c/214456-q-ordering-sorting-category-text-propertygrid

I don't believe there is a way to do this. The only thing that I could
find that indicates you might be able to do this is the PropertySort
property. If you set it to none, it says that the properties are displayed
in the order that they are received from the type descriptor. You might be
able to create a proxy type descriptor between your object and the
propertygrid, which would then return not only the properties in the correct
order, but the properties with the categories in the order that you want
them in...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文