在WPF中使用WinForm控件方法
1、????? 首先添加對如下兩個dll文件的引用:WindowsFormsIntegration.dll小渊,System.Windows.Forms.dll它浅。
2畴嘶、????? 在要使用WinForm控件的WPF窗體的XAML文件中添加如下內(nèi)容:
xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
xmlns:wfi ="clr-namespace:System.Windows.Forms.Integration;assembly=WindowsFormsIntegration"
3、?????? 在WPF的容器控件內(nèi)如StackPanel內(nèi)首先要添加WinForm控件的宿主容器,用于銜接WPF和WinForm.
對應(yīng)XAML如下:
????<StackPanel>
? ? ? ? ? ? <wfi:WindowsFormsHost>
? ? ? ? ? ? ? ? <wf:Label x:Name="wfLabel" Text="winForm控件在此" />
? ? ? ? ? ? </wfi:WindowsFormsHost>
? ? ? ? ? ? <wfi:WindowsFormsHost>
? ? ? ? ? ? ? ? <wf:Button x:Name="wfButton" Text="確定" Click="wfButton_Click" />
? ? ? ? ? ? </wfi:WindowsFormsHost>
? ? ? ? ? ? <Button Content="Button" Margin="10" Name="button1"/>
? ? ? ? </StackPanel>
4、?????? 如果要在WPF后臺代碼中訪問上面的Lable嫉父,可直接像在WinForm中使用一樣。如在點擊某一按鈕時改變Lable內(nèi)容眼刃,代碼如下:wpfLabel.Text=”內(nèi)容已改變”;
在WinForm中使用WPF控件方法
1绕辖、????? 首先添加對WPF所需的程序集,
2擂红、?在項目中新建一個用戶控件或引用一個用戶控件.
3仪际、????? 重新生成項目,將 工具箱自動生成的WPF 控件拖入Winform中
3、?在WinForm后臺代碼可修改對應(yīng)的WPF控件.
4篮条、?Demo效果