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
try UIElement.InvalidateVisual();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With