WPF“魔法” 在 PropertyGrid 控件中
我开始了一个业余爱好项目来了解 WPF,在我的研究中我偶然发现了这个 WPF PropertyGrid http://www.codeplex .com/wpg
我从 http://wpg.codeplex.com 下载源代码/SourceControl/ListDownloadableCommits.aspx ,并开始浏览它,看起来很酷,而且有效。 但对于我的一生,我无法弄清楚它是如何运作的。 以下问题是我乍一看留下的问题。
- 它如何知道对象具有哪些属性?
- 它如何渲染控件来编辑值?
- 它如何决定渲染哪个控件? (我猜测属性的元数据描述)
我了解 DependancyProperties 处理新值之间的数据交换。
- 发挥所有作用的 XAML 或 CodeBehind 代码在哪里?
Default.xaml 文件中有很多 XAML 代码,但据我所知,它只是在那里定义的样式和外观。
I started a hobby project to learn about WPF and in my reasearch i stumpled upon this WPF PropertyGrid http://www.codeplex.com/wpg
I downloaded the source from http://wpg.codeplex.com/SourceControl/ListDownloadableCommits.aspx
, And started browsing through it, looks cool, and works. But for the life of me, I can't figure out HOW it works. The following questions is what I'm left with at first glance.
- How does it know what properties an object have?
- How does it render the control to edit a Value?
- How does it decide what control to render? (i'm guessing meta data descriptions on the properties)
I understand the DependancyProperties handles the dataexchange between new values.
- Where is the XAML or CodeBehind code that does all the magic?
There is lots of XAML code in the Default.xaml file, but as far as I can tell it is only styles and the looks that are defined there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果它的工作方式与 Windows 窗体属性网格相同,它将执行以下操作:
对于其他问题...好吧,我没有看过代码,所以我无法回答;)
If it works the same way as the Windows Forms property grid, it does the following :
For the other questions... well, I haven't looked at the code, so I can't answer ;)
它使用
System.ComponentModel.TypeDescriptor
通过TypeDescriptor.GetProperties
。PropertyDescriptor
提供有关每个属性的大量信息(例如,PropertyDescriptor.IsReadOnly
会告诉您属性是只读的)。 此外,使用PropertyDescriptor.GetValue
和PropertyDescriptor.SetValue
,可以获取并写入属性的值。它使用名为
PropertyGrid
的自定义控件,该控件公开Item
的ObservableCollection
。Item
只是核心模型对象(即Property
)的基类。Property
公开属性的基础类型 (PropertyDescriptor.PropertyType
),并公开PropertyDescriptor.GetValue
和PropertyDescriptor.SetValue
> 通过Property.Value
的方法。 反过来,这也是启用编辑所必需的。DataTemplate
s 是关键在这里。 一些 CLR 类型具有自定义DataTemplate
来呈现您看到的控件。 例如,Boolean
类型呈现为CheckBox
,而Enum
呈现为ComboBox
。 默认的DataTemplate
是一个TextBox
。它的倾向在
Themes\Default.xaml
中。代码可以总结如下。 数据(
Data\Property
)封装了对象的属性信息,并提供属性(Property.Value
)来读写属性的值。 此数据在自定义控件 (PropertyGrid
) 中作为ObservableCollection
公开,该控件使用Themes\Default.xaml 中的
。DataTemplate
进行呈现不要忽视
DataTemplate
。It uses
System.ComponentModel.TypeDescriptor
to determine the properties that a type has viaTypeDescriptor.GetProperties
. APropertyDescriptor
provides a wealth of information about each property (e.g.,PropertyDescriptor.IsReadOnly
will tell you that a property is readonly). Further, usingPropertyDescriptor.GetValue
andPropertyDescriptor.SetValue
, the values of properties can be obtained and written to.It uses a custom control called a
PropertyGrid
which exposes anObservableCollection
ofItem
s.Item
is merely a base class for the core model object which isProperty
.Property
exposes the underlying type (PropertyDescritor.PropertyType
) of the property and exposes thePropertyDescriptor.GetValue
andPropertyDescriptor.SetValue
methods viaProperty.Value
. This, in turn, is what is bound to to enable the editing.DataTemplate
s are the key here. A few CLR types have customDataTemplate
s that render the controls you see. For example, theBoolean
type is rendered as aCheckBox
while anEnum
is rendered as aComboBox
. The defaultDataTemplate
is aTextBox
.The propensity of it is in
Themes\Default.xaml
.The code can be summarized as thus. There is data (
Data\Property
) that encapsulates the information about properties of an object and provides a property (Property.Value
) to read and write the value of a property. This data is exposed as anObservableCollection
in a custom control (PropertyGrid
) which is rendered usingDataTemplate
s inThemes\Default.xaml
.Don't overlook the
DataTemplate
s.