WPF“魔法” 在 PropertyGrid 控件中

发布于 2024-07-30 02:57:58 字数 676 浏览 4 评论 0原文

我开始了一个业余爱好项目来了解 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 技术交流群。

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

发布评论

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

评论(2

舂唻埖巳落 2024-08-06 02:57:58
  • 它如何知道对象具有哪些属性?

如果它的工作方式与 Windows 窗体属性网格相同,它将执行以下操作:

  • 如果对象实现 ICustomTypeDescriptor,则从此接口获取属性(使用 GetProperties 方法),
  • 否则,使用对象类型的反射 (obj.GetType() .GetProperties()

对于其他问题...好吧,我没有看过代码,所以我无法回答;)

  • How does it know what properties an object have?

If it works the same way as the Windows Forms property grid, it does the following :

  • if the object implements ICustomTypeDescriptor, obtain the properties from this interface (with the GetProperties method)
  • otherwise, use reflection on the object's type (obj.GetType().GetProperties()

For the other questions... well, I haven't looked at the code, so I can't answer ;)

相对绾红妆 2024-08-06 02:57:58
  • 它如何知道对象具有哪些属性?

它使用 System.ComponentModel.TypeDescriptor 通过 TypeDescriptor.GetPropertiesPropertyDescriptor 提供有关每个属性的大量信息(例如,PropertyDescriptor.IsReadOnly 会告诉您属性是只读的)。 此外,使用 PropertyDescriptor.GetValuePropertyDescriptor.SetValue,可以获取并写入属性的值。

  • 它如何呈现控件来编辑值?
  • 它如何决定渲染哪个控件? (我猜测属性的元数据描述)

它使用名为 PropertyGrid 的自定义控件,该控件公开 ItemObservableCollectionItem 只是核心模型对象(即 Property)的基类。 Property 公开属性的基础类型 (PropertyDescriptor.PropertyType),并公开 PropertyDescriptor.GetValuePropertyDescriptor.SetValue > 通过 Property.Value 的方法。 反过来,这也是启用编辑所必需的。 DataTemplates 是关键在这里。 一些 CLR 类型具有自定义 DataTemplate 来呈现您看到的控件。 例如,Boolean 类型呈现为 CheckBox,而 Enum 呈现为 ComboBox。 默认的DataTemplate 是一个TextBox

  • 发挥所有作用的 XAML 或 CodeBehind 代码在哪里?

它的倾向在 Themes\Default.xaml 中。

代码可以总结如下。 数据(Data\Property)封装了对象的属性信息,并提供属性(Property.Value)来读写属性的值。 此数据在自定义控件 (PropertyGrid) 中作为 ObservableCollection 公开,该控件使用 Themes\Default.xaml 中的 DataTemplate 进行呈现

Default.xaml 文件中有很多 XAML 代码,但据我所知,它只是其中定义的样式和外观。

不要忽视DataTemplate

  • How does it know what properties an object have?

It uses System.ComponentModel.TypeDescriptor to determine the properties that a type has via TypeDescriptor.GetProperties. A PropertyDescriptor provides a wealth of information about each property (e.g., PropertyDescriptor.IsReadOnly will tell you that a property is readonly). Further, using PropertyDescriptor.GetValue and PropertyDescriptor.SetValue, the values of properties can be obtained and written to.

  • 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)

It uses a custom control called a PropertyGrid which exposes an ObservableCollection of Items. Item is merely a base class for the core model object which is Property. Property exposes the underlying type (PropertyDescritor.PropertyType) of the property and exposes the PropertyDescriptor.GetValue and PropertyDescriptor.SetValue methods via Property.Value. This, in turn, is what is bound to to enable the editing. DataTemplates are the key here. A few CLR types have custom DataTemplates that render the controls you see. For example, the Boolean type is rendered as a CheckBox while an Enum is rendered as a ComboBox. The default DataTemplate is a TextBox.

  • Where is the XAML or CodeBehind code that does all the magic?

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 an ObservableCollection in a custom control (PropertyGrid) which is rendered using DataTemplates in Themes\Default.xaml.

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.

Don't overlook the DataTemplates.

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