WPF Rectangle does not have a Click event

后端 未结 4 791
南旧
南旧 2020-12-15 05:00

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 be

相关标签:
4条回答
  • 2020-12-15 05:01

    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.

    0 讨论(0)
  • 2020-12-15 05:04

    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!

    0 讨论(0)
  • 2020-12-15 05:14

    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>
    
    0 讨论(0)
  • 2020-12-15 05:27

    Rectangle has event Tapped, which works fine. In designing universal apps for Windows 8.1, there are new events. Tapped, DoubleTaped, RightTapped and Holding.

    0 讨论(0)
提交回复
热议问题