C# 如何将WinForm用户控件添加到WPF中,以便我可以在xaml.cs文件中引用它
我正在将WinForms项目的一部分迁移到WPF中 我想将现有的WinForms用户控件添加到WPF表单中。WinForm用户控件称为“TicketPrinter”,与WPF表单位于同一个项目中 在我的xaml中,我有这样一行:C# 如何将WinForm用户控件添加到WPF中,以便我可以在xaml.cs文件中引用它,c#,wpf,winforms,visual-studio-2010,user-controls,C#,Wpf,Winforms,Visual Studio 2010,User Controls,我正在将WinForms项目的一部分迁移到WPF中 我想将现有的WinForms用户控件添加到WPF表单中。WinForm用户控件称为“TicketPrinter”,与WPF表单位于同一个项目中 在我的xaml中,我有这样一行: xmlns:Printers="clr-namespace:Project.UserControls.Printers" 然后我在我的xaml中使用它: <WindowsFormsHost Height="430" HorizontalAlign
xmlns:Printers="clr-namespace:Project.UserControls.Printers"
然后我在我的xaml中使用它:
<WindowsFormsHost Height="430" HorizontalAlignment="Left" Margin="468,12,0,0" Name="windowsFormsHost1" VerticalAlignment="Top" Width="324">
<Printers:TicketPrinter Printers:Name="ZapTicketPrinter">
</Printers:TicketPrinter>
</WindowsFormsHost>
</Grid>
</Window>
但是得到一个空值
我错过了什么?
如何在代码中引用名称?提供x:name而不是printer:name
<WindowsFormsHost>
<Printers:TicketPrinter x:Name="ZapTicketPrinter"/>
</WindowsFormsHost>
MSDN样品
使用代码隐藏使用xaml
尝试使用以下代码:
private void LoadWFUserControl()
{
// Initialize a Host Control which allows hosting a windows form control on WPF. Ensure that the WindowsFormIntegration Reference is present.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create an object of your User control.
MyWebcam uc_webcam = new MyWebcam();
// Assign MyWebcam control as the host control's child.
host.Child = uc_webcam;
// Add the interop host control to the Grid control's collection of child controls. Make sure to rename grid1 to appr
this.grid1.Children.Add(host);
}
private void LoadWFUserControl()
{
// Initialize a Host Control which allows hosting a windows form control on WPF. Ensure that the WindowsFormIntegration Reference is present.
System.Windows.Forms.Integration.WindowsFormsHost host =
new System.Windows.Forms.Integration.WindowsFormsHost();
// Create an object of your User control.
MyWebcam uc_webcam = new MyWebcam();
// Assign MyWebcam control as the host control's child.
host.Child = uc_webcam;
// Add the interop host control to the Grid control's collection of child controls. Make sure to rename grid1 to appr
this.grid1.Children.Add(host);
}