Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 enter image description here

like image 432
Hiber Avatar asked Apr 18 '12 09:04

Hiber


1 Answers

try UIElement.InvalidateVisual();

like image 177
Nathan Avatar answered Sep 20 '22 18:09

Nathan