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

相关推荐
Magnum Lehar6 小时前
wpf3d游戏引擎ControlTemplate.xaml.cs文件实现
游戏引擎·wpf
董先生_ad986ad7 小时前
C# 解析 URL URI 中的参数
前端·c#
xiaowu08012 小时前
C# 中的Async 和 Await 的用法详解
java·开发语言·c#
Magnum Lehar14 小时前
wpf游戏引擎前端的Transform.cs实现
前端·游戏引擎·wpf
董先生_ad986ad15 小时前
MVVM模式中,BaseViewModel 的 IsBusy 属性的作用
c#
@Crazy Snail16 小时前
WPF数据绑定疑惑解答--(关于控件的Itemsource,Collection绑定)
windows·wpf·wpf数据绑定
刚子编程16 小时前
C# WinForms 实现打印监听组件
开发语言·c#·winform·打印监听组件
猕员桃17 小时前
《Elasticsearch 分布式搜索在聊天记录检索中的深度优化》
分布式·elasticsearch·wpf
Magnum Lehar17 小时前
wpf3d游戏引擎前端ControlTemplate实现
前端·游戏引擎·wpf
时央12345618 小时前
C#使用Tuple方法实现OpreateResultModel功能
运维·开发语言·c#