2

I am working on a WPF application that has a Panel within a WindowsFormHost. The panel contains graphics that need to be redrawn every so often, and the code that draws these graphics is located within an OnPaint() event. The problem is, the OnPaint() event never seems to fire. To debug, I added a button to my form and used the button's click event handler to call the Invalidate() event. Even when I call Invalidate(), I can't seem to get the Paint event to fire. My code-behind looks like this:

public MainWindow() 
{
    InitializeComponent();
}

private void myPanel_Paint(object sender, System.Windows.Forms.PaintEventArgs e) 
{
    /// Draw stuff
} 

private void Button_Click(object sender, RoutedEventArgs e)
{
    myPanel.Invalidate();
}

And my XAML looks like this:

<Window x:Class="MyProject.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:wf="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms"
    Title="MainWindow" Height="350" Width="525">
<Grid>
    <StackPanel Name="stackPanel">
        <Button Content="Button" Click="Button_Click"/>
        <WindowsFormsHost x:Name="windowsFormsHost" >
            <WindowsFormsHost.Child>
                <wf:Panel x:Name="myPanel" Paint="myPanel_Paint"/>
            </WindowsFormsHost.Child>
        </WindowsFormsHost>
    </StackPanel>
</Grid>

I've read through this: WindowsFormHost Paint Event Not Firing

...but we don't seem to be having the same problem, as my XAML does reference my Paint event handler, and OnPaint() still doesn't fire.

I have already tried adding myPanel.Update() to my Button_Click event, beneath my call to myPanel.Invalidate(). That also doesn't work.

What am I doing wrong here? Is it possible that Invalidate() is not really invalidating?

Community
  • 1
  • 1
chelsea
  • 131
  • 1
  • 1
  • 10
  • 2
    Works just fine. It will probably help if you can actually *see* the panel, set the host's Height to, say, 200. And you'll have to use the Random class in the Paint event handler to actually see a difference. – Hans Passant Jun 17 '15 at 21:53
  • This question might help. http://stackoverflow.com/questions/9198298/custom-paint-handler-on-a-winforms-control-inside-a-wpf-application – Matt Rowland Jun 18 '15 at 00:28

0 Answers0