c# - 重绘 WPF 控件(图像控件)时如何引发事件?
问题描述
我有一个处理图片框的绘制事件的 winforms 绘制事件处理程序。正如绘制事件描述所说,“......当重绘控件时触发事件”。我不太明白这一点,我希望在 WPF 中的 Image 控件上引发相同的事件。但我找不到任何此类事件。这是winforms代码
我如何在 WPF 中做到这一点?
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
if (pictureBox1.Image != null)
{
if (temprect != new Rectangle())
{
e.Graphics.DrawRectangle(new Pen(selectionBrush, 2), temprect);
}
}
else
{
using (Font myFont = new Font("Arial", 40, FontStyle.Bold))
{
e.Graphics.DrawString("No Image", myFont, Brushes.LightGray,
new Point(pictureBox1.Width / 2 - 132, pictureBox1.Height / 2 - 50));
}
}
}
我已经使用 DrawingContext 类将事件 Hanlder 中的所有代码转换为 WPF。现在,我只需要对可以引发“重绘图像控件时”的事件的帮助。
解决方案
WPF 不使用 WinForm 的按需模式绘画。每当布局系统希望元素“重绘”自身时,都会调用a的OnRender
方法。UIElement
你可以在你的类中重写这个方法:
public class YourElement : FrameworkElement
{
protected override void OnRender(DrawingContext dc)
{
base.OnRender(dc);
}
}
如果要显式重新渲染元素,可以调用该InvalidateVisual()
方法。
推荐阅读
- r - 处理 R 中的阻塞数据
- php - 数据库:从每个数组结果的同一列中获取两个字段
- processing - 两个物体的法向力
- odata - SAPUI5 OData v2:从 m.Table 中删除行
- css - 覆盖主 style.css 文件中的左侧菜单字体和位置
- android - Android Mapbox Markers - 只有第一个添加的标记是可点击的
- mysql - 为什么这个 select 语句这么慢?
- r - 求和匹配R中两个不同数据框列中的两个字符串的实例
- javascript - 几乎每次按下按钮都会在短时间内通过每次按下按钮创建 100 个 ajax 请求
- android - 如何在传递额外数据时使用 Intent 完成 AsyncTask?