Search code examples
c#wpfxamlbackgroundwindowsformshost

How to Set BackgroundImage for Button in WindowsFormsHost?


How can I set background image on button in WindowsFormsHost?

<WindowsFormsHost Name="wfbutton1" Width="200" Height="100" Background="White" Visibility="Visible" >
    <wf:Button Height="23" Left="145" Top="127" wf:Name="button1" Width="75" 
               Click="button1_Click" Visible="True" FlatStyle="Flat"
               BackgroundImage="C:\\Users\\Kvint\\Desktop\\Background.bmp" />
</WindowsFormsHost >

Solution

  • Give your button a name like any other WPF element:

    <WindowsFormsHost ... >
        <wf:Button x:Name="button1" ... />
    </WindowsFormsHost >
    

    Then reference that in the code-behind:

    button1.BackgroundImage
        = System.Drawing.Image.FromFile(@"C:\Users\Kvint\Desktop\Background.bmp");
    

    If you're looking for a purely XAML approach, I'll leave that to someone else. I'm not sure it's even possible in this case.