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
WPF Rectangle does not have a Click event
提问by M. Dudley
回答by Kent Boogaart
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?
如果您对MouseDown
and不满意MouseUp
,也许您可以将Rectangle
a放入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>