English 中文(简体)
如何将 WinForm 用户控制添加到 WPF 中, 以便我可以在 xaml. cs 文件中引用它
原标题:How to add WinForm User Control Into WPF so that I can reference it in the xaml.cs file

我正在把WinForms项目的一部分 迁移到WPF。

我想将现有的 WinForms 用户控制器添加到 WPF 表格中。 WinForms 用户控制器被称为“ TicketPrinter ”, 并且与 WPF 表格一样, 并生活在同一个项目中 。

我的Xaml里有一条线:

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>

当我运行项目时, 用户控制会像预期的那样出现在表格上。

但当我进入Xaml.cs文件背后的代码 并尝试访问“ZapTicketPrinter”时,它无法作为参考。

或(或)

我尝试使用 ZapTicketPrinter, 但它不被识别 。

我还尝试了以下做法:

TicketPrinter ticketPrinter = this.FindName("ZapTicketPrinter") as TicketPrinter;

无效

What am I missing? How do I reference the name in my code?

最佳回答

提供 x: Name 代替打印机:name

<WindowsFormsHost>
    <Printers:TicketPrinter x:Name="ZapTicketPrinter"/>
</WindowsFormsHost>

MSDN 样本

Using code behind
http://msdn.microsoft.com/en-us/library/ms751761.aspx
Walkthrough: Hosting a Windows Forms Control in WPF

Using xaml
http://msdn.microsoft.com/en-us/library/ms742875.aspx
Walkthrough: Hosting a Windows Forms Control in WPF by Using 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);
}




相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签