Add this code to your control:
/// <summary>
/// This override is necessary to avoid a performance bug in WPF.
/// </summary>
/// <param name="hitTestParameters"></param>
/// <returns></returns>
protected override HitTestResult HitTestCore(PointHitTestParameters hitTestParameters) {
Point pt = hitTestParameters.HitPoint;
return new PointHitTestResult(this, pt);
}
public static bool Contains(Rect a, Rect b) {
return a.X <= b.X && b.X+b.Width <= a.X+a.Width && a.Y <= b.Y && b.Y+b.Height <= a.Y+a.Height && a.Width >= 0 && a.Height >= 0;
}
public static bool Intersects(Rect a, Rect b) {
double tw = a.Width;
double rw = b.Width;
double tx = a.X;
double rx = b.X;
if (!Double.IsPositiveInfinity(tw) && !Double.IsPositiveInfinity(rw)) {
tw += tx;
rw += rx;
if (Double.IsNaN(rw) || Double.IsNaN(tw) || tx > rw || rx > tw) return false;
}
double th = a.Height;
double rh = b.Height;
double ty = a.Y;
double ry = b.Y;
if (!Double.IsPositiveInfinity(th) && !Double.IsPositiveInfinity(rh)) {
th += ty;
rh += ry;
if (Double.IsNaN(rh) || Double.IsNaN(th) || ty > rh || ry > th) return false;
}
return true;
}
/// <summary>
/// This override is necessary to avoid a performance bug in WPF.
/// </summary>
/// <param name="hitTestParameters"></param>
/// <returns></returns>
protected override GeometryHitTestResult HitTestCore(GeometryHitTestParameters hitTestParameters) {
Geometry geo = hitTestParameters.HitGeometry;
if (geo != null) {
double w = this.ActualWidth;
double h = this.ActualHeight;
Rect a = new Rect(0, 0, w, h);
Rect b = geo.Bounds;
IntersectionDetail detail = IntersectionDetail.Empty;
if (Intersects(a, b)) {
if (Contains(b, a)) detail = IntersectionDetail.FullyInside;
else if (Contains(a, b)) detail = IntersectionDetail.FullyContains;
else detail = IntersectionDetail.Intersects;
}
return new GeometryHitTestResult(this, detail);
}
return base.HitTestCore(hitTestParameters);
}