如何在WPF中调用Winform控件

原文地址:http://hi.baidu.com/stuoopluwqbbeod/item/32ec38403da42ee2bcf45167

功能实现主要分三步:
1、添加两个引用:WindowsFormsIntegration.dll (负责整合WPF和Windows)、System.Windows.Forms.
2、在 XAML文件中添加两个引用(粗体部分):

<Window x:Class="CrossBowDemo.MainWindow"
     xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
     xmlns:wf ="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
     xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
     xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
     Title="Hosting Windows Forms Control In WPF" 
     Height="300" 
     Width="650" 
     ResizeMode="NoResize"
     Loaded="WindowLoadedHandler" 
     >

</Window>

3、在XAML编码区实现你想添加的控件:

    原文添加的是 DataGridView控件:
   <wfi:WindowsFormsHost>
       <!-- Set some properties on Windows Forms control in Xaml -->
      <wf:DataGridView x:Name="dataGridView" Dock="Fill" SelectionMode="FullRowSelect"/>
    </wfi:WindowsFormsHost>
效果图:



本人添加的是 NumericUpDown控件:

    <Grid Height="0" Margin="146,0,0,116" MinHeight="20" MinWidth="20" Name="grid1" VerticalAlignment="Bottom" HorizontalAlignment="Left" Width="50">
      <wfi:WindowsFormsHost>
        <wf:NumericUpDown x:Name="nupCounter" Maximum="100"></wf:NumericUpDown>
      </wfi:WindowsFormsHost>
    </Grid>
效果图:



在本人的代码中Grid的作用相当于Web页面中用来布局的Table。 此处加上Grid是为了方便移动控件的位置。

 

原文地址:http://hi.baidu.com/stuoopluwqbbeod/item/32ec38403da42ee2bcf45167

posted @   kumat  阅读(622)  评论(0编辑  收藏  举报
编辑推荐:
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
点击右上角即可分享
微信分享提示