WPF 矩形没有 Click 事件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1068979/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 20:39:16  来源:igfitidea点击:

WPF Rectangle does not have a Click event

wpfeventsshapes

提问by M. Dudley

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

似乎 WPF矩形形状没有定义 Click 事件。我应该用什么代替?

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

它确实有 MouseUp,但它的行为并不完全相同。

回答by Kent Boogaart

If you're not happy with MouseDownand MouseUp, perhaps you could just put the Rectanglein a Buttonand handle the Button's Clickevent?

如果您对MouseDownand不满意MouseUp,也许您可​​以将Rectanglea放入aButton并处理Button'sClick事件?

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

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

这真的取决于你所追求的行为。如果需要,请详细说明。

回答by Grokys

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

要将单击处理添加到 Rectangle 本身,您可以使用InputBindings属性:

<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!

这将导致在单击矩形时执行 FooCommand。如果您使用的是 MVVM,则非常方便!

回答by mjhamre

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

我正在寻找相关的 DoubleClick 事件并遇到了这个建议,即简单地将感兴趣的对象嵌入到 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>

回答by Qerts

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

矩形有事件Tapped,它工作正常。在为 Windows 8.1 设计通用应用程序时,有一些新事件。Tapped、DoubleTaped、RightTapped 和 Hold。