是否可以按照声明的方式在 UI 中显示 PropertyGrid 项目?我发现它们首先按 CategoryAttribute 排序,然后按 DisplayName 属性升序排序。
我正在通过 Visual Studio 2010 Ultimate 使用 .NET 版本 3.5。
编辑
该应用程序是 WPF 应用程序。
来自此文档:https://learn.microsoft.com/en-US/dotnet/api/system.windows.forms.propertysort
如果将属性
PropertySort
设置为 PropertySort.NoSort
,属性的排序顺序应遵循以下标准: 属性按照从 TypeDescriptor 中检索的顺序显示。
您可以使用注释设置相当多的属性,其中之一是“显示顺序” 使用 System.ComponentModel.DataAnnotations.Display 属性将如下所示:
[DisplayName("Error"),Display(Order = 5)]
public string Error { get; internal set; }
你可以试试这个代码。
private void propertyGrid1_PropertySortChanged(object sender, EventArgs e)
{
if (propertyGrid1.PropertySort == PropertySort.CategorizedAlphabetical)
{
propertyGrid1.PropertySort = PropertySort.Categorized;
}
}
同上 [System.Windows.Forms.PropertySort]::NoSort 但请注意,如果您使用 Property 参数创建控件,并在该参数的哈希表中设置此 PropertySort 和 SelectedObject,则它可能仍具有按字母顺序排列的第一个项目作为 SelectedGridItem 并滚动网格以显示该项目。 为了确保新的(原始订单的第一个)属性是 SelectedGridItem,可以在创建控件后设置 SelectedObject 或为您的控件设置},您可以使用: }.SelectedGridItem = }.SelectedGridItem.ParentGridEntry.Children[0]