日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

WPF 中如何使用第三方控件 ,可以使用WindowsFormsHost 类

發布時間:2025/3/13 windows 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 WPF 中如何使用第三方控件 ,可以使用WindowsFormsHost 类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

允許在 WPF 頁面上承載 Windows Forms控件的元素。

命名空間:? ?System.Windows.Forms.Integration

程序集:? ?WindowsFormsIntegration(在 WindowsFormsIntegration.dll 中) 用于 XAML 的 XMLNS:http://schemas.microsoft.com/winfx/2006/xaml/presentation, http://schemas.microsoft.com/netfx/2007/xaml/presentation

?

add: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.integration.windowsformshost

如果要引用的話

<Window x:Class="Selection.Window1" ??? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ??? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ??? xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" ??? Title="Dates" Height="314" Width="373"> ??? <Grid> ??? <Label HorizontalAlignment="Left" Margin="20,25,0,0" Name="label1" Width="35.63" Height="23.2766666666667" VerticalAlignment="Top">First</Label> ??? <Label Height="23.2766666666667" HorizontalAlignment="Left" Margin="20,56,0,0" Name="label2" VerticalAlignment="Top" Width="52.63">Second</Label> ?? ?<WindowsFormsHost Name="hostFirst" Margin="85,25,18,0" Height="23.2766666666667" VerticalAlignment="Top"> ????? <wf:DateTimePicker Name="first"/> ??? </WindowsFormsHost> ??? <WindowsFormsHost Name="hostSecond" Margin="85,56,18,0" Height="23.2766666666667" VerticalAlignment="Top"> ?? ?? <wf:DateTimePicker Name="second"/>//文本框里是日期 ??? </WindowsFormsHost> ??? <Button Height="23" HorizontalAlignment="Left" Margin="20,100,0,0" Name="compare" VerticalAlignment="Top" Width="75" Click="compareClick">Compare</Button> ??? <TextBox Margin="20,131,104,20" Name="info" TextWrapping="WrapWithOverflow" AcceptsReturn="False" IsReadOnly="True" /> ??? <Button Height="22" HorizontalAlignment="Right" Margin="0,0,18,20" Name="quit" VerticalAlignment="Bottom" Width="75" Click="quitClick">Quit</Button> ? </Grid> </Window>

后臺C#

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows; using System.Windows.Controls; using System.Windows.Data; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; using System.Windows.Forms;

namespace Selection { ??

? /// <summary> ??

? /// Interaction logic for Window1.xaml ??? /

// </summary>
??? public partial class Window1 : Window ??? { ?????

?? private DateTimePicker first; ???????

private DateTimePicker second;
??????? public Window1() ??????? { ?????

?????? InitializeComponent();

//

?Child獲取或設置由 WindowsFormsHost 元素承載的子控件。 ?????

??????????? first = hostFirst.Child as DateTimePicker; ???????

???? second = hostSecond.Child as DateTimePicker; ??????? }
??????? private void quitClick(object sender, RoutedEventArgs e) ??????? { ????????

??? this.Close(); ????

??? }
??????? private void compareClick(object sender, RoutedEventArgs e) ??????? { ??????

????? int diff = dateCompare(first.Value, second.Value); ??????

????? info.Text = ""; ???????????

show("first == second", diff == 0); ?????????

?? show("first != second", diff != 0); ?????????

?? show("first <? second", diff < 0); ???????????

show("first <= second", diff <= 0); ??????????

? show("first >? second", diff > 0); ??????????

? show("first >= second", diff >= 0); ??????? }
??????? private void show(string exp, bool result) ??????? { ??????

????? info.Text += exp; ???????

???? info.Text += " : " + result.ToString(); ?????

?????? info.Text += "\r\n";

?

?

?//\r 表示:回車符(ACSII:13 或0x0d),就是我們常說的硬回車。 ?????????

? //? \n 表示:換行(ACSII:10 或0x0a),就是我們常說的軟回車。 ???

???????? //\n,好比你在DreamWeaver里做一個網頁,在源代碼里按一下回車,是給源代碼換行。
??????? }
??????? private int dateCompare(DateTime leftHandSide, DateTime rightHandSide) ??????? { ?????

?????? // TO DO ???????????

return 42; ??

????? } ??

? }

}

?

?

WPF Windows Forms integration???????????

?

在wpf程序中整合windows form:

1.在references中添加WindowsFormsIntegration和System.Windows.Forms。

2.在xaml中使用的時候要寫清楚名字空間,可以把這兩個ns定義出來。

?下面兩句是重點,wpf書寫的時候一定要注意引用

xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"

xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"

插入WindowsFormsControl:

<wfi:WindowsFormsHost> ??????????? <wf:DateTimePicker/> </wfi:WindowsFormsHost>

?

看到Windows.Forms下面也有Button控件(廢話),于是想知道這個Button和WPF通常用的System.Windows.Controls.Button有什么不同。

看了看兩者的繼承體系:

System.Windows.Controls域中:? Button<ButtonBase<ContentControl<Control

System.Windows.Forms域:???????? Button<ButtonBase<Control

看似差不多,其實他們完全存在于兩個不同的空間,沒有一點聯系。

兩個Button類里定義的內容也不盡相同,Forms.Button看上去內容豐富一些,發現里面有DoubleClick這個event,但是試了試并不起作用,換到windows form 程序下仍然不起作用。(Visual Studio的property界面上看不到這個事件,但是可以在代碼里自己加event handler,雖然加了也白加)。Controls.Button里雖然沒有DoubleClick,但是MouseDoubleClick是work的。而Forms.Button似乎就只能觸發Click事件。

<Window x:Class="WpfApplication1.Window1" ??? xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ??? xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ??? Title="Window1" Height="300" Width="300"> ?

?? <Grid xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" ???????

?? xmlns:wfi="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration" ????????? > ??

????? <Grid.RowDefinitions> ???????????

<RowDefinition Height="*"/> ???????????

<RowDefinition Height="*"/> ???????

</Grid.RowDefinitions> ??

????? <Button Grid.Row="1" MouseDoubleClick="Button_MouseDoubleClick">button</Button> ???????

<wfi:WindowsFormsHost Grid.Row="0"> ???????????

<wf:Button MouseDoubleClick="Button_MouseDoubleClick"/> ????

??? </wfi:WindowsFormsHost> ??? </Grid> </Window>

?

這兩個事件對應的event類型也不一樣,一個是System.Windows.Input.MouseButtonEventHandler,一個是System.Windows.Forms.MouseEventHandler,所以對應的event handler的函數的參數也不一樣,一個是private void Button_MouseDoubleClick(object sender, MouseButtonEventArgs e),一個是private void Button_MouseDoubleClick(object sender, System.Windows.Forms.MouseEventArgs e),因此兩個event handler函數名可以相同。

轉載于:https://www.cnblogs.com/1175429393wljblog/p/4892181.html

與50位技術專家面對面20年技術見證,附贈技術全景圖

總結

以上是生活随笔為你收集整理的WPF 中如何使用第三方控件 ,可以使用WindowsFormsHost 类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。