5

Is there possible to make hit test in ellipse bound rectangle, like on this image?

enter image description here

Bradley Uffner
  • 16,641
  • 3
  • 39
  • 76
slavka
  • 119
  • 6

1 Answers1

6

you can put them both into a border grid and check if it was clicked

XAML:

            <Grid MouseDown="Border_MouseDown">
                <Rectangle Width="100"
                           Height="100"
                           Fill="Green" />
                <Ellipse Width="100"
                         Height="100"
                         Fill="Orange" />
            </Grid>

Code behind

   private void Border_MouseDown(object sender, MouseButtonEventArgs e)
        {
            MessageBox.Show("hit it");
        }

EDIT Just to make it complete here comes the XAML for green regions only:

   <Grid>
            <Rectangle Width="100"
                       Height="100"
                       Fill="Green"
                       MouseDown="Border_MouseDown" />
            <Ellipse Width="100"
                     Height="100"
                     Fill="Orange" />
        </Grid>
Mat
  • 1,960
  • 5
  • 25
  • 38
  • Won't that fire if clicked inside the orange area? He wants it to only fire if in the green area. – Scott Chamberlain Sep 26 '16 at 14:26
  • this was not clear for me from the question itself. it says the rectangle bounds... – Mat Sep 26 '16 at 14:28
  • @ScottChamberlain I've added the XAML code to answer also your interpretation of the question. – Mat Sep 26 '16 at 14:32
  • @ScottChamberlain "He wants it to only fire if in the green area". How do you know? IMO "I need hittest in this green region" means "I need hittest *also* in this green region". – Clemens Sep 26 '16 at 22:28
  • @Mat Why the Border? You could as well attach the MouseDown handler to the Grid. You wouldn't even need the Rectangle, as you could simply set the Grid's Background. – Clemens Sep 26 '16 at 22:32
  • @slavka do you like this answer? maybe you want to accept it? – Mat Sep 27 '16 at 12:23
  • @Mat, thank you for advices. Yes, your solution is works, but can not be applied in my project (I'm can't create container, that contains other controls). I'm newbie in WPF, and I thought that there is an more easier way, than determine control under cursor by MouseMove event. Thank you, and sorry for my english. :) – slavka Sep 28 '16 at 15:37
  • @Mat, as I say, I'm newbie in WPF. In my case, the ellipse background (green area), must be transparent. I thought, that in you approach, if "background" rectangle is transparent, a MouseMove event don't fires. I'm just check this, and I'm wrong. I will do as you say. Thank you again! – slavka Sep 29 '16 at 05:34