简体   繁体   中英

WPF UIElement refresh bug?

I have a custom panel, which is used to draw selection effect; but sometimes it does't clear the previous rectangles, if the mouse is moved back and forth when the screen is big enough(across two monitors), is it a WPF bug or limitation? Do you know how to solve this issue? Thanks in advance.

The simplified code looks like the following

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;
    }
}        

and I put it in a grid and invalidate it during mouse move, like this

 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));

    }

and the result looks like the following picture 在此输入图像描述

尝试UIElement.InvalidateVisual();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM