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控件驻留。

相关推荐
IT规划师29 分钟前
C#|.net core 基础 - 扩展数组添加删除性能最好的方法
c#·.netcore·数组
时光追逐者1 小时前
分享6个.NET开源的AI和LLM相关项目框架
人工智能·microsoft·ai·c#·.net·.netcore
friklogff1 小时前
【C#生态园】提升C#开发效率:深入了解自然语言处理库与工具
开发语言·c#·区块链
__water10 小时前
『功能项目』回调函数处理死亡【54】
c#·回调函数·unity引擎
__water10 小时前
『功能项目』眩晕图标显示【52】
c#·unity引擎·动画事件
__water11 小时前
『功能项目』第二职业法师的平A【57】
c#·unity引擎·魔法球伤害传递
__water13 小时前
『功能项目』战士的伤害型技能【45】
c#·unity引擎·战士职业伤害型技能
君莫愁。14 小时前
【Unity】检测鼠标点击位置是否有2D对象
unity·c#·游戏引擎
Lingbug15 小时前
.Net日志组件之NLog的使用和配置
后端·c#·.net·.netcore
咩咩觉主15 小时前
Unity实战案例全解析:PVZ 植物卡片状态分析
unity·c#·游戏引擎