C# WPF WindowsFormsHost control not exist in code

64 Views Asked by At

I added a winforms custom control on my wpf window. The control shows in ui but when i trying to reach it inside code, im getting this error;

"The name 'myCustomControl' does not exist in the current context"

Here is my XAML code;

 <WindowsFormsHost Width="300" Height="300" Grid.Row="1">
        <my:MyControl Dock="Fill" Name="myCustomControl"></my:MyControl>
 </WindowsFormsHost>

Sorry for my english

1

There are 1 best solutions below

0
Clemens On BEST ANSWER

You have to set x:Name instead of Name.

In contrast to WPF, setting the Name property of a WinForms control does not automatically generate a field to access the control.

<WindowsFormsHost ...>
    <my:MyControl x:Name="myCustomControl" .../>
</WindowsFormsHost>