我认为 Microsoft 认为在 WPF 中包含 PropertyGrid 控件没有任何意义,因为创建自己的控件非常简单,而且如果他们创建了控件,则更难设置样式。
要创建您自己的 PropertyGrid,只需将 a与<ListBox>
带有停靠在左侧的 a 作为属性名称和 a作为值编辑器一起使用,然后在属性上启用分组。<ItemsTemplate>
<DockPanel>
<TextBlock>
<ContentPresenter>
Category
您需要编写的唯一代码是反映对象并创建属性列表的代码。
这是您将使用的粗略概念:
DataContext =
from pi in object.GetType().GetProperties()
select new PropertyGridRow
{
Name = pi.Name,
Category = (
from attrib in pi.GetCustomAttributes(false).OfType<CategoryAttribute>()
select attrib.Category
).FirstOrDefault() ?? "None",
Description = (
from attrib in pi.GetCustomAttributes(false).OfType<DescriptionAttribute>()
select attrib.Description
).FirstOrDefault(),
Editor = CreateEditor(pi),
Object = object,
};
CreateEditor 方法将简单地为属性构造一个适当的编辑器,并绑定到实际的属性值。
在 XAML 中,<ListBox.ItemTemplate>
将是这样的:
<DataTemplate>
<DockPanel>
<TextBlock Text="{Binding PropertyName}" Width="200" />
<ContentPresenter DataContext="{Binding Object}" Content="{Binding Editor}" />
</DockPanel>
</DataTemplate>
我会让你填写其余的细节。