Can I make WPF set IsMouseOver for both the covering and covered element?

后端 未结 3 1506
刺人心
刺人心 2021-01-21 01:20

The simplified example this; picture a Venn diagram made from two elements, A and B, that overlap. If I mouse over (A AND (NOT B)) all of A lights up. If I mouse over (B AND (NO

3条回答
  •  -上瘾入骨i
    2021-01-21 01:49

    You can do manual hit testing using VisualTreeHelper. This can go into a MouseMove handler on some parent object. Here I'm assuming a Venn diagram made of ellipses named RedCircle and BlueCircle:

    bool overRed = false;
    bool overBlue = false;
    if (BlueCircle.IsMouseOver || RedCircle.IsMouseOver)
    {
        HitTestParameters parameters = new PointHitTestParameters(e.GetPosition(RedCircle));
        VisualTreeHelper.HitTest(RedCircle, new HitTestFilterCallback(element => HitTestFilterBehavior.Continue), result =>
        {
            if (result.VisualHit == RedCircle)
                overRed = true;
            return HitTestResultBehavior.Continue;
        }, parameters);
    
        parameters = new PointHitTestParameters(e.GetPosition(BlueCircle));
        VisualTreeHelper.HitTest(BlueCircle, new HitTestFilterCallback(element => HitTestFilterBehavior.Continue), result =>
        {
            if (result.VisualHit == BlueCircle)
                overBlue = true;
            return HitTestResultBehavior.Continue;
        }, parameters);
    }
    

提交回复
热议问题