Error updating WPF UIElement?

I have a custom panel that is used to draw a selection effect; but sometimes it doesn’t clear the previous rectangles, if the mouse moves back and forth when the screen is large enough (via two monitors), is this an error or a limitation of WPF? Do you know how to solve this problem? Thanks in advance.

The simplified code is as follows

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 the grid and invalidate it while moving the mouse, for example

  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 is as follows: enter image description here

+6
source share
1 answer

try UIElement.InvalidateVisual();

+1
source

Source: https://habr.com/ru/post/913506/


All Articles