Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Rectangle does not have a Click event

Tags:

events

wpf

shapes

It seems that the WPF Rectangle shape does not have the Click event defined. What am I supposed to use instead?

It does have MouseUp, but it's not quite the same behavior.

like image 907
sourcenouveau Avatar asked Jul 01 '09 12:07

sourcenouveau


3 Answers

If you're not happy with MouseDown and MouseUp, perhaps you could just put the Rectangle in a Button and handle the Button's Click event?

<Button>     <Button.Template>         <ControlTemplate>             <Rectangle .../>         </ControlTemplate>     </Button.Template> </Button> 

It really depends with the behavior you're after. Please elaborate if needs be.

like image 127
Kent Boogaart Avatar answered Sep 25 '22 23:09

Kent Boogaart


To add click handing to a Rectangle itself, you can use the InputBindings property:

<Rectangle Fill="Blue" Stroke="Black">     <Rectangle.InputBindings>         <MouseBinding Gesture="LeftClick" Command="{Binding FooCommand}"/>     </Rectangle.InputBindings> </Rectangle> 

This will cause the FooCommand to be executed when the rectangle is clicked. Handy if you're using MVVM!

like image 22
Grokys Avatar answered Sep 22 '22 23:09

Grokys


I was looking for the related DoubleClick event and came across this suggestion to simply embed the object of interest in a ContentControl.

Here is their example (with a border, which also did not support click/double click).

<ContentControl MouseDoubleClick="OnDoubleClick">
    <Border Margin="10" BorderBrush="Black" BorderThickness="2">
        <Grid Margin="4">
            <Rectangle Fill="Red" />
            <TextBlock Text="Hello" FontSize="15" />
        </Grid>
    </Border>
</ContentControl>
like image 45
mjhamre Avatar answered Sep 23 '22 23:09

mjhamre