我认为微软没有看到点包括在WPF中PropertyGrid控件,因为它是那么微不足道创建自己的,如果他们创造了控制这将是更难的风格。
要创建自己的PropertyGrid中,只使用一个<ListBox>
与<ItemsTemplate>
有含停靠到左侧的属性名称<TextBlock>
和值编辑器<ContentPresenter>
,然后启用对Category
财产集合了<DockPanel>
。
您需要编写的唯一代码是反映该对象并创建属性列表的代码。
这里是你会用什么一个粗略的想法:
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>
我会告诉你的细节填写其余。