Search code examples
c#wpfwindowsformshost

C# WPF WindowsFormsHost control not exist in code


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


Solution

  • 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>