c# - WPF UIElement 刷新错误?

标签 c# .net wpf wpf-controls uielement

我有一个自定义面板,用来绘制选区效果;但有时它不会清除以前的矩形,如果在屏幕足够大(跨两个显示器)时来回移动鼠标,这是 WPF 错误或限制吗?你知道如何解决这个问题吗?提前致谢。

简化后的代码如下所示

public class CustomPanel : Panel
{
    private Rectangle _rectangle;

    public CustomPanel()
    {
        this._rectangle = new Rectangle();
        this._rectangle.StrokeThickness = 3;
        this._rectangle.Stroke = new SolidColorBrush(Color.FromArgb(220, 0, 0, 0)); ;
        this.Children.Add(this._rectangle);
    }            

    protected override Size MeasureOverride(Size availableSize)
    {
        this._rectangle.Measure(availableSize);
        return this._rectangle.DesiredSize;
    }

    protected override Size ArrangeOverride(Size finalSize)
    {
        if (!finalSize.IsEmpty)
        {
            this._rectangle.Arrange(new Rect(new Point(0, 0), finalSize));
        }
        return finalSize;
    }
}        

然后我把它放在一个网格中,并在鼠标移动时使它无效,就像这样

 void OnMouseMove(object sender, MouseEventArgs e)
    {
        var point = e.GetPosition(this);
        var size = new Size(point.X>=0? point.X:0, point.Y>=0? point.Y:0);
        this.Selection.Measure(size);
        this.Selection.Arrange(new Rect(size));

    }

结果如下图所示 enter image description here

最佳答案

试试 UIElement.InvalidateVisual();

关于c# - WPF UIElement 刷新错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10206171/

相关文章:

c# - Entity Framework 与 ASP.NET WebForms 应用程序的最佳实践

c# - 从流畅的流文件 (.ism) 中获取缩略图

c# - 在更新外键字段之前释放 DataContext

.net - 我应该如何在这段 winform 代码中使用 IoC?

c# - 如何处理从另一个 AppDomain 抛出的异常?

c# - 在没有注册表的情况下加载 IFilter

.net - 如何在只执行一次命令时检查 DBNull?

c# - .NET 毛伊岛 : Check checkbox when clicking on label

wpf - 在 WPF 中打开 Power Bi 报表

WPF:如何在 View 中创建 ViewModel