WPF与WindowsForms的交互

1,在WPF中调用WinForm及其控件。

1.1,在WPF中调用WinFrom窗口。

调用模态Winform窗口,可正常进行交互,无异常。

cs 复制代码
private void btnOpenWin_Click(object sender, RoutedEventArgs e)
        {
            FrmMsg win = new FrmMsg();
            win.ShowDialog();
        }

调用非模态WinForm窗口,交互能力受限,winform窗口不能接收一些键盘信息如Tab键的信息。如要正常交互则需要进行如下处理。

引入程序集:WindowsFormsIntegration.dll

cs 复制代码
private void btnOpenWin_Click(object sender, RoutedEventArgs e)
        {
            WindowsFormsHost.EnableWindowsFormsInterop();
            FrmMsg win = new FrmMsg();
            win.ShowDialog();
        }

1.2,优化WinForm窗口的显示风格。

Wpf调用的Winform窗口,其默认为XP风格,如果需要优化其风格需进行如下操作。

cs 复制代码
public partial class App : Application
    {
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);
            //优化winform在Wpf中显示样式
            System.Windows.Forms.Application.EnableVisualStyles();
        }
    }

1.3,在Xaml中引入Winform控件。

添加命名空间

XML 复制代码
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

使用Wpf的WindowsFormsHost元素包裹WinForm控件。

XML 复制代码
<WindowsFormsHost Grid.Row="2">
            <!--引用wpf中没有的数字显示框-->
            <wf:NumericUpDown></wf:NumericUpDown>
        </WindowsFormsHost>

2,在Winform中调用Wpf窗口及其控件。

2.1,添加Wpf窗口。

WinForm中不能直接创建Wpf窗口,需要通过添加现有项模式加载已有的Wpf窗口。

2.2,在WinForm中调用Wpf窗口。

模态调用的Wpf窗口,可正常进行交互。

cs 复制代码
 private void btnOpenWin_Click(object sender, RoutedEventArgs e)
        {         
             WpfWin win = new WpfWin();
            win.ShowDialog();
        }

非模态调用Wpf窗口。默认情况下非模态调用的Wpf窗口无法接收键盘信息即无法交互,必须进行如下操作才可正常交互。

引用程序集:WindowsFormsIntegration.dll

cs 复制代码
private void button1_Click(object sender, EventArgs e)
        {
           WpfWin win = new WpfWin();
            //需要添加对win的引用否则win非模态窗口不能接收键盘信息,模态不存在这个问题
            ElementHost.EnableModelessKeyboardInterop(win);
            win.Show();
            
        }

2.4,Winform中添加Wpf自定义控件

WinForm中可直接创建Wpf的自定义控件,创建完自定义控件编译后通过WinForm的ElementHost控件驻留自定义的Wpf控件。

自定义的控件:

XML 复制代码
<UserControl x:Class="WinForm中调用WPF窗口.MyWPFControl"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:WinForm中调用WPF窗口"
             mc:Ignorable="d" 
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <Grid.RowDefinitions>
            <RowDefinition Height="27*"/>
            <RowDefinition Height="25*"/>
            <RowDefinition Height="18*"/>
            <RowDefinition Height="30*"/>
        </Grid.RowDefinitions>
        <Slider Maximum="100" Minimum="0" x:Name="slider01"></Slider>
        <TextBox Grid.Row="1" Margin="5" Background="AliceBlue" Text="{Binding ElementName=slider01, Path=Value}"></TextBox>
        <Button Margin="10" Content="提交" Grid.Row="2"></Button>
    </Grid>
</UserControl>

通过WinForm的ElementHost控件驻留。

相关推荐
小羊没烦恼!4 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
神仙别闹4 天前
基于C#+MySQL实现(WinForm)个人聊天室软件
c#
kybs19914 天前
全球灾害数据分析可视化 毕业设计-附源码66794
vue.js·spring boot·mysql·安全·django·c#·asp.net
Polevne5 天前
C# SFTP客户端实现文件传输
c#·ssh
samble5 天前
从零搭建灌装监控系统(四):深色主题与样式系统,ResourceDictionary 统一管理
c#·wpf·mvvm·样式·工业控制
_Jonas5 天前
.net framework开发环境正式环境配置文件的切换
.net·wpf
cjp5605 天前
024 . WPF MVVM类封装优化
c#
淡海水5 天前
13-04-面试-源码级深度追问链
数据结构·unity·面试·c#·游戏引擎·源码·il2cpp
asdzx675 天前
如何使用 C# 提取 PPT 文本与图片
c#·ppt·提取文本
2501_930707785 天前
使用C#代码使用条件格式为 Excel 隔行设置颜色
c#·excel