在 WPF 中使用 .NET 2.0 (Windows Forms) 控件的限制?
我想在 WPF 上启动一个新应用程序。 WPF 中的新用户界面需要 DataGridView 控件和 PropertyGrid 控件。但看起来这两个控件在 WPF 中不存在,我想使用 WindowsFormsHost
托管这两个控件。
但是,如果我这样做,有人会预见到这种方法有任何限制吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
主要限制是您失去了 WPF 的所有强大功能:
数据绑定
、ControlTemplates
和DataTemplates
、无限大小调整、缩放/旋转、<代码>不透明度,<代码>附加属性,仅举几例。需要放弃的东西太多了!您必须使用旧的、乏味且容易出错的 WinForms 技术对这些控件进行编程,并再次处理您多年前摆脱的所有限制。DataGridView
NET Framework 3.5 sp1 有一个
DataGrid
可以完成这项工作,并且有几个第三方控件可以完成此任务,例如来自 Xceed 的控件。使用基于 WPF 的网格可以在网格内实现完整的数据绑定、模板化和样式化,而如果使用 WinForms 的 DataGridView,则这是不可能的。PropertyGrid
WPF 没有附带
PropertyGrid
的原因是,使用 WPF 已经提供的功能来重新创建非常容易:一个简单的列表框就可以了,样式正确,使用只需几行隐藏代码。使用 WPF
PropertyGrid
实现的优点在于,您可以使用模板来呈现正在编辑的属性,最重要的是,您可以通过使用一些绑定在 XAML 中表达它们来添加新的属性编辑器。例如,我们的属性网格之一中的某些属性是用滑块设置的,只需大约五行 XAML 即可实现这一点。下面是一些代码,说明了在 WPF 中实现 PropertyGrid 背后的关键概念:
通过这些代码,可以非常快速、轻松地用几行 XAML 复制
PropertyGrid
的整个外观: 只需使用 ListBox按类别分组,以及由DockPanel
组成的ItemTemplate
,其中包含绑定到属性名称的固定宽度TextBlock
和ContentPresenter
打印属性编辑器。The main limitation is that you loose all the powerful features of WPF:
Data binding
,ControlTemplates
andDataTemplates
, Infinite sizing, Zooms/Rotations,Opacity
,Attached Properties
, just to name a few. That's a lot to give up! You'll have to program these controls using the old tedious and error-prone WinForms techniques, and deal again with all those limitations you freed yourself from years ago.DataGridView
NET Framework 3.5 sp1 has a
DataGrid
that may do the job, and there are several of third-party controls for this such as the one from Xceed. Using a WPF-based grid allows complete data binding, templating and styling inside the grid which would not be possible if you use WinForms' DataGridView.PropertyGrid
The reason WPF doesn't come with a
PropertyGrid
is that it is so easy to recreate using what WPF already gives you: A simple listbox will do, properly styled, with only a few lines of code-behind.The beauty in using a WPF
PropertyGrid
implementation is that you can use templates to present the properties you are editing, and most importantly you can add new property editors by just expressing them in XAML with a few bindings. For example, some of the properties in one of our property grids are set with sliders, and it was only about five lines of XAML to get that to happen.Here is some code illustrating the key concepts behind implementing a PropertyGrid in WPF:
With this it is very quick and easy to replicate the entire look and feel of
PropertyGrid
with a few lines of XAML: Just use a ListBox with grouping by Category, and anItemTemplate
that consists of aDockPanel
containing a fixed widthTextBlock
bound to the property name and aContentPresenter
to print the property editor.首先,有第三方属性网格和数据 用于 WPF 的网格,这些通常支持 WPF 的附加样式功能,并且更适合 WPF 应用程序。因此,您不需要退回到 WinForms,而且可能也不应该这样做,除非确实无法选择第三方或工具包控件。
如果您确实必须返回到 WinForms,则需要注意的主要限制是“空域规则”:您不能让 WinForms 和 WPF 控件相互重叠。另外,当然,WinForms 控件无法参与 WPF 数据绑定,并且必须使用 old-skool 过程代码来解决,但您可以将它们封装在用户控件中来解决此问题。另外,如果您尝试制作奇特的视觉效果,那么它们可能不太适合 WinForms 控件:如果您在 WinForms 控件附近进行变换或动画,空域规则可能会给您带来很大的麻烦。但对于视觉上简单的应用程序来说,它们应该可以正常工作。
First off, there are third party property grids and data grids for WPF, and these will typically support the additional styling capabilities of WPF and fit in more idiomatically with WPF applications. So you don't need to drop back to WinForms, and probably shouldn't unless third party or toolkit controls really aren't an option.
If you do have to drop back to WinForms, the main limitation to be aware of is the "airspace rule": you can't have WinForms and WPF controls overlapping each other. Plus of course WinForms controls can't participate in WPF data binding and will have to be addressed using old-skool procedural code, though you could encapsulate them in user controls to work around this. Also, if you're trying to do fancy visual effects then they may not work too well with the WinForms controls: the airspace rule is likely to bite you hard if you are doing transforms or animations in the vicinity of a WinForms control. But for visually simple applications they should work okay.
WPF 工具包中有一个 Datagrid 控件。 CodePlex 上还有第三方 PropertyGrid 控件(在非常宽松的 MS-PL 许可证下)。
There's a Datagrid control in the WPF Toolkit. There's also a third party PropertyGrid control on CodePlex as well (under the very permissive MS-PL license).