在WPF使用.NET 2.0(Windows窗体)控件的限制? [英] Limitations of using .NET 2.0 (Windows Forms) controls in WPF?

查看:217
本文介绍了在WPF使用.NET 2.0(Windows窗体)控件的限制?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 WPF 上启动新的应用程序。 WPF中的新用户界面需要 DataGridView 控件和 PropertyGrid 控件。但是看起来这两个控件不会存在于WPF中,我想使用 WindowsFormsHost 来托管这两个控件。

I want to start a new application on WPF. The new User interface in WPF needs DataGridView control and PropertyGrid Control. But it looks like that these two controls won't exist in WPF and I want to host these two controls using WindowsFormsHost.

但是,如果我这样做,是否有任何限制,任何人都可以通过这种方式来看?

However, if I do that, is there any limitation anybody forsee with this approach?

推荐答案

主要的局限是您松开了WPF的所有强大功能:数据绑定 ControlTemplates DataTemplates ,无限尺寸,缩放/旋转,不透明度附加属性,仅举几例。这是很多要放弃!您将不得不使用旧的乏味且易出错的WinForms技术对这些控件进行编程,并再次处理您从几年前解脱出来的所有限制。

The main limitation is that you loose all the powerful features of WPF: Data binding, ControlTemplates and DataTemplates, 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有一个 DataGrid 可以做这个工作,几个第三方的控件,比如Xceed。使用基于WPF的网格允许在网格内完成数据绑定,模板和样式,如果您使用WinForms的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

WPF不附带一个 PropertyGrid 的原因在于,使用WPF已经给你:一个简单的列表框将会做,正确的样式,只有几行代码隐藏。

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.

使用WPF的美丽 PropertyGrid 实现是,您可以使用模板来呈现正在编辑的属性,最重要的是,您可以通过在XAML中使用一些绑定来表达它们来添加新的属性编辑器。例如,我们的一个属性网格中的一些属性是用滑块设置的,只有大约五行XAML才能实现。

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.

这里是一些代码说明了在WPF中实现PropertyGrid的关键概念:

Here is some code illustrating the key concepts behind implementing a PropertyGrid in WPF:

public class PropertyGrid
{
  ...
  public static readonly DependencyProperty SelectedObjectProperty = ...
  {
    PropertyChangedCallback = (obj, e) =>
    {
      PropertyItems =
        from pi in SelectedObject.GetType().GetProperties()
        select new PropertyGridItem { Object = SelectedObject, PropertyInfo = pi };
    }
  }
}

public class PropertyInfo
{
  public object Object;
  public PropertyInfo PropertyInfo;
  public object Value
  {
    get { return PropertyInfo.GetValue(Object); }
    set { PropertyInfo.SetValue(Object, value); }
  }
  public string Category
  {
    get
    {
      return (
        from attrib in PropertyInfo.GetCustomAttributes().OfType<CategoryAttribute>()
        select attrib.Name
      ).FirstOrDefault();
    }
  }
}

这样很快并且容易地复制具有几行XAML的 PropertyGrid 的整个外观和感觉:只需使用按类别分组的列表框,以及 ItemTemplate 包含绑定到属性名称的 DockPanel ,其中包含固定宽度 TextBlock code> ContentPresenter 打印属性编辑器。

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 an ItemTemplate that consists of a DockPanel containing a fixed width TextBlock bound to the property name and a ContentPresenter to print the property editor.

这篇关于在WPF使用.NET 2.0(Windows窗体)控件的限制?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆