C# 调用Invalidate()时不会触发绘制事件
我正在开发一个WPF应用程序,它在C# 调用Invalidate()时不会触发绘制事件,c#,wpf,events,paintevent,C#,Wpf,Events,Paintevent,我正在开发一个WPF应用程序,它在WindowsFormHost中有一个面板。该面板包含需要经常重新绘制的图形,绘制这些图形的代码位于OnPaint()事件中。问题是,OnPaint()事件似乎从未触发。为了进行调试,我在表单中添加了一个按钮,并使用该按钮的click事件处理程序调用Invalidate()事件。即使在调用Invalidate()时,我似乎也无法触发绘制事件。我的代码如下所示: public MainWindow() { InitializeComponent(); }
WindowsFormHost
中有一个面板。该面板包含需要经常重新绘制的图形,绘制这些图形的代码位于OnPaint()
事件中。问题是,OnPaint()
事件似乎从未触发。为了进行调试,我在表单中添加了一个按钮,并使用该按钮的click事件处理程序调用Invalidate()事件。即使在调用Invalidate()
时,我似乎也无法触发绘制事件。我的代码如下所示:
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();
}
<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>
我的XAML如下所示:
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();
}
<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>
我通读了以下内容:
…但是我们似乎没有遇到相同的问题,因为我的XAML引用了我的Paint事件处理程序,并且OnPaint()
仍然没有触发
我已经尝试将myPanel.Update()
添加到我的按钮中,单击调用myPanel.Invalidate()
下面的事件。那也不行
我做错了什么?有没有可能Invalidate()
不是真的在失效 很好用。如果你能真正看到面板,将主机的高度设置为,比如说,200,可能会有所帮助。您必须在Paint事件处理程序中使用Random类才能真正看到差异。这个问题可能会有所帮助。