winform和WPF控件使用、事件调用上的不同

一、控件使用上

1.button

winform中button显示的内容可以通过button.Text来get、set

button.Text = "连接";
string str = button.Text;

WPF中改成了button.Content,并且Content的get返回值类型为object

button.Content = "连接";
string str = button.Content.ToString();

二、事件调用上

1.winform存在"ShowForm.designer.cs"文件,如下图所示

自己写的窗口关闭的时间函数,需在该文件内手动添加调用

this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.ShowForm_Closed);

2.WPF没有上面的文件,取而代之的是“MainWindow.g.i.cs”,下文是对应的代码,不过该文件的内容是自动生成的。

#line 9 "..\..\MainWindow.xaml"
            ((Skip_Hole.MainWindow)(target)).Closing += new System.ComponentModel.CancelEventHandler(this.Window_Closing);

自己要添加或者修改的话,需要在“MainWindow.xaml”上修改,如下

<Window x:Class="Skip_Hole.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:Skip_Hole"
        mc:Ignorable="d"
        Title="MainWindow" Height="350" Width="525"
        Closing="Window_Closing">

在Window的属性里增加“Closing="Window_Closing"”

WPF调用WinForm控件是通过将WinForm控件嵌入到WPF内容中实现的。首先,需要在WPF项目中引用`WindowsFormsIntegration`命名空间。 1. 创建WinForm控件:在WinForm项目中创建所需的WinForm控件,例如一个Windows.Forms.Button。 2. 在WPF中添加WindowsFormsHost控件:在WPF的XAML文件中,添加一个WindowsFormsHost控件。 ```xaml <Grid> <WindowsFormsHost Name="windowsFormsHost1" /> </Grid> ``` 3. 将WinForm控件添加到WindowsFormsHost控件中:在WPF的代码文件中,在相关的事件处理程序或页面加载完成等事件中,将WinForm控件添加到WindowsFormsHost控件中。 ```csharp private void Window_Loaded(object sender, RoutedEventArgs e) { System.Windows.Forms.Button winFormButton = new System.Windows.Forms.Button(); windowsFormsHost1.Child = winFormButton; } ``` 通过以上步骤,就可以将WinForm控件嵌入到WPF中了。需要注意的是,由于WPFWinForm使用不同的UI渲染技术,嵌入的WinForm控件的外观可能会与周围的WPF控件不完全一致,需要进行一些样式调整来保持一致性。 同时,还可以通过WPF的命令模型和事件模型与嵌入的WinForm控件进行交互。例如,可以通过WPF的命令绑定和事件处理程序来响应WinForm控件的点击事件,实现业务逻辑的处理。 总而言之,通过WindowsFormsHost控件可以在WPF中嵌入WinForm控件,并通过WPF的命令和事件模型来与其进行交互,提供更丰富的用户界面和功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值