week 10

the first thing that we need to do (after creating a new WPF project) is add a few references. You can do this by by right-clicking on the references folder in the solution explorer, and choosing "Add Reference":

Then you will get a dialog like this:

There are two .NET components you will want to add - first, System.Windows.Forms, and then, all the way at the bottom (after you get to this dialog a second time),WindowsFormsIntegration.

Once those two references are added, we have access to all of the WinForms controls, and access to the components that will allow us to embed them in WPF. So now lets get started on some code. First, we are going to take a look at embedding theDataGridView using C#, so this means our XAML is going to be extremely simple:

<Window x:Class="WinFormsInWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="Using WinForms In WPF" Height="300" Width="300">
  <Grid x:Name="_Container">
    <Grid.RowDefinitions>
      <RowDefinition Height="*"></RowDefinition>
      <RowDefinition Height="20"></RowDefinition>
    </Grid.RowDefinitions>
    <Button Grid.Row="1" Click="ClearClick" HorizontalAlignment="Right">
      Clear All Rows
    </Button>
  </Grid>
</Window>

As you might notice, there is no reference to the DataGridView anywhere in that XAML. All we have is a grid with two rows, and a button in the second row. This is because we are going to be shoving in the DataGridView into the first row using C# code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Forms.Integration;

namespace WinFormsInWPF
{
  public partial class Window1 : Window
  {
    private System.Windows.Forms.DataGridView _MyDataGrid;
    private WindowsFormsHost _MyHost;

    public Window1()
    {
      InitializeComponent();

      _MyHost = new WindowsFormsHost();
      _MyDataGrid = new System.Windows.Forms.DataGridView();

      System.Windows.Forms.DataGridViewColumn col;
      col = new System.Windows.Forms.DataGridViewColumn();
      col.CellTemplate = new System.Windows.Forms.DataGridViewTextBoxCell();
      col.Name = "Col 1";
      _MyDataGrid.Columns.Add(col);
      col = new System.Windows.Forms.DataGridViewColumn();
      col.CellTemplate = new System.Windows.Forms.DataGridViewTextBoxCell();
      col.Name = "Col 2";
      _MyDataGrid.Columns.Add(col);
      _MyDataGrid.Rows.Add(new object[] { "Item 1", "Foo" });
      _MyDataGrid.Rows.Add(new object[] { "Item 2", "Bar" });

      _MyHost.Child = _MyDataGrid;
      _Container.Children.Add(_MyHost);
    }

    private void ClearClick(object sender, RoutedEventArgs e)
    {
      _MyDataGrid.Rows.Clear();
    }
  }
}

 

here in the constructor (after the InitializeComponent call - we want all our XAML created stuff to be initialized first), we make a new WindowsFormsHost (which is from the namespace System.Windows.Forms.Integration). This object is the glue layer between WPF and WinForms. The WindowsFormsHost is a FrameworkElement, so it can be added to anything in WPF that can take an element. But it has a property Child which takes in asystem.Windows.Forms.Control - and this is the control that will be embedded.

So we create the WindowsFormsHost and the DataGridView, and then populate theDataGridView with some info. You might be wondering why there is no usingstatement for System.Windows.Forms - and instead all the references are explicit. This is because there are some conflicts between the System.Windows.Forms and the standard WPF namespaces - so if you do add a using statement for System.Windows.Forms, it is very likely ambiguous references will start to crop up in your code. So it is just safer and easier to explicitly reference the System.Windows.Forms classes.

After the DataGridView is all set up, we add it as the child for the WindowsFormsHost, and then we add the WindowsFormsHost as a child of our Grid. Interacting with theDataGridView in code works exactly as you might expect - for instance, the ClearClickmethod which clears the current rows in the DataGridView when the WPF button is clicked. The interaction on the user side of things also now pretty much just works, although there can be some quirks with focus and input that you may have to deal with (and will probably be specific to your particular situation). Here is a picture of the sample app in action:

Now onto how do this in XAML, instead of using C#. Here is our new XAML code:

<Window x:Class="WinFormsInWPF.Window1"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:WinForms="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    Title="Using WinForms In WPF" Height="300" Width="300">
  <Grid x:Name="_Container">
    <Grid.RowDefinitions>
      <RowDefinition Height="*"></RowDefinition>
      <RowDefinition Height="20"></RowDefinition>
    </Grid.RowDefinitions>
    <WindowsFormsHost Grid.Row="0">
      <WinForms:DataGridView x:Name="_MyDataGrid">
      </WinForms:DataGridView>
    </WindowsFormsHost>
    <Button Grid.Row="1" Click="ClearClick" HorizontalAlignment="Right">
      Clear All Rows
    </Button>
  </Grid>
</Window>

As you can see, it got a bit more complicated. First, we added a new xmlns attribute to the Window tag. This pulls in the System.Windows.Forms under the prefix WinForms. The other change is the addition of the WindowsFormsHost component. We add it just like any other WPF FrameworkElement, but inside we get to declare a WinForms control - in this case a DataGridView. We give our DataGridView a name so we can refer to it later, and that‘s it for the XAML side. Clean, isn‘t it?

And the C# side gets cleaned up as well:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WinFormsInWPF
{
  public partial class Window1 : Window
  {
    public Window1()
    {
      InitializeComponent();

      System.Windows.Forms.DataGridViewColumn col;
      col = new System.Windows.Forms.DataGridViewColumn();
      col.CellTemplate = new System.Windows.Forms.DataGridViewTextBoxCell();
      col.Name = "Col 1";
      _MyDataGrid.Columns.Add(col);
      col = new System.Windows.Forms.DataGridViewColumn();
      col.CellTemplate = new System.Windows.Forms.DataGridViewTextBoxCell();
      col.Name = "Col 2";
      _MyDataGrid.Columns.Add(col);
      _MyDataGrid.Rows.Add(new object[] { "Item 1", "Foo" });
      _MyDataGrid.Rows.Add(new object[] { "Item 2", "Bar" });
    }

    private void ClearClick(object sender, RoutedEventArgs e)
    {
      _MyDataGrid.Rows.Clear();
    }
  }
}

As you can see, all we have to worry about is the filling of the DataGridView with some data (and it still needs to come after the InitializeComponent call - otherwise nothing will have been created yet). The only downside to using XAML to embed the WinForms control is that you don‘t have any control over the constructor - the default no argument constructor is always used. Other than that, its nice and keeps the data/display separation paradigm intact.

To host the MaskedTextBox control

  1. Create a WPF Application project named HostingWfInWpfWithXaml.
  2. Add references to the following assemblies.
    • WindowsFormsIntegration
    • System.Windows.Forms
  3. Open MainWindow.xaml in the WPF Designer.
  4. In the Window element, add the following namespace mapping. The wf namespace mapping establishes a reference to the assembly that contains the Windows Forms control.

    XAML

    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    
  5. In the Grid element add the following XAML.

    The MaskedTextBox control is created as a child of the WindowsFormsHost control.

    XAML

    	<Grid>
    
    		<WindowsFormsHost>
    			<wf:MaskedTextBox x:Name="mtbDate" Mask="00/00/0000"/>
    		</WindowsFormsHost>
    
    	</Grid>
    
  6. Press F5 to build and run the application.
时间: 2024-08-09 07:31:48

week 10的相关文章

百度粉红色风科技上来看积分

http://www.ebay.com/cln/508gua_gvqjq/-/167266747010/2015.02.10 http://www.ebay.com/cln/jhu2290/-/167423283013/2015.02.10 http://www.ebay.com/cln/cha.m22/-/167166250017/2015.02.10 http://www.ebay.com/cln/fenyu56/-/167382503016/2015.02.10 http://www.eb

百度附件是分开就爱死了开发了

http://www.ebay.com/cln/m_m3154/-/167249028014/2015.02.10 http://www.ebay.com/cln/zhsu412/-/167238372018/2015.02.10 http://www.ebay.com/cln/mi.han5/-/167545028015/2015.02.10 http://www.ebay.com/cln/lij5252/-/167389481016/2015.02.10 http://www.ebay.co

使用 Chrome 浏览器插件 Web Scraper 10分钟轻松实现网页数据的爬取

本文标签: WebScraper Chrome浏览器插件 网页数据的爬取 使用Chrome 浏览器插件 Web Scraper 可以轻松实现网页数据的爬取,不写代码,鼠标操作,点哪爬哪,还不用考虑爬虫中的登陆.验证码.异步加载等复杂问题. Web Scraper插件 Web Scraper 官网中的简介: Web Scraper Extension (Free!)Using our extension you can create a plan (sitemap) how a web site

Install Hyper-V on Windows 10

? Enable Hyper-V to create virtual machines on Windows 10.Hyper-V can be enabled in many ways including using the Windows 10 control panel, PowerShell (my favorite) or using the Deployment Imaging Servicing and Management tool (DISM). This documents

10.4 补充范围内丢失的值

select y.yr.coalesce(x.cnt,0) as cntfrom (select min_year-mod(cast(min_year as int),10)+rn as yrfrom (select (select min(extract(year from hreadate))from emp) as min_year,id-1 as rnfrom t10) a) yleft join(select extract(year from hredate) as yr,count

10.6 监控io性能 - 10.7 free命令 - 10.8 ps命令 - 10.9 查看网络状态 - 10.10 linux下抓包

- 10.6 监控io性能 - 10.7 free命令 - 10.8 ps命令 - 10.9 查看网络状态 - 10.10 linux下抓包 - 扩展tcp三次握手四次挥手 http://www.doc88.com/p-9913773324388.html  - tshark几个用法:http://www.aminglinux.com/bbs/thread-995-1-1.html  # 10.6 监控io性能 ![mark](http://oqxf7c508.bkt.clouddn.com/b

Windows 10 UWP程序标题栏设置

原文:Windows 10 UWP程序标题栏设置 在Windows 10程序中,以前只能用于全屏方式的Metro程序现在可以运行在窗口模式下了,并且改了个新名字,叫Windows 通用程序(Universal Windows app),简称UWP程序.新的UWP程序虽然大体上还是和以前的Metro程序差不多的,但还是引入了一点新东西的,本文这里就介绍一下它的标题栏设置的几个特性. 隐藏标题栏: 将应用界面扩展至 Titlebar 区域 CoreApplication.GetCurrentView

How to enable C development in a Windows 10 development environment VM

To enable C development in a Windows 10 development environment VM, follow these steps: Start VS in the Windows 10 development environment VM. Choose "File" -> "New" -> "Project", choose "Open Visual Studio Install

设置UWP程序自启动(Automate launching Windows 10 UWP apps)

原文:设置UWP程序自启动(Automate launching Windows 10 UWP apps) 在开发UWP程序的过程中,有时候需要设置程序的自启.本人实现的步骤如下: 1.在VS中激活Protocol (Package.appxmanifest --> Declarations --> Add Protocol),图示如下: 2.编译并发布项目(Build and Deploy) 发布之后Protocol被激活,在(控制面板 --> 程序 --> 默认程序 -->

10大APP界面框架设计模式详解

随着移动互联网的发展,移动app已经成为了每个互联网公司的标配了,那作为产品经理,我们如何设计出更加符合用户体验的app产品呢?今天和大家分享的就是10中最常见的app界面光甲设计模式,一起来看看吧. 1.标签导航 标签导航是十大界面框架设计里最常用的界面框架设计,也是被业界之内公认的一种普遍使用的页面框架设计.那么这种页面框架设计在作业方面对一个用户来说也是最常见的一种页面框架设计,比如说微博.微信.手机百度.支付宝.淘宝,这些我们所谓的超级APP都是运用的标签导航,无一例外.从这个角度也可以