Paint event does not fire when Invalidate() is called

1.6k views Asked by At

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?

0

There are 0 answers