看起来 WPF 的 矩形(Rectangle) 形状没有定义 Click 事件。那么我该使用什么替代方法呢?
虽然它具有 MouseUp,但行为不完全相同。
如果您对 MouseDown
和 MouseUp
不满意,也许您可以将 Rectangle
放入一个 Button
中,并处理该 Button
的 Click
事件?
<Button>
<Button.Template>
<ControlTemplate>
<Rectangle .../>
</ControlTemplate>
</Button.Template>
</Button>
这实际上取决于您想要的行为。 如有需要,请详细说明。
如果要向一个矩形本身添加点击处理,可以使用InputBindings属性:
<Rectangle Fill="Blue" Stroke="Black">
<Rectangle.InputBindings>
<MouseBinding Gesture="LeftClick" Command="{Binding FooCommand}"/>
</Rectangle.InputBindings>
</Rectangle>
当矩形被点击时,这将导致FooCommand被执行。如果你正在使用MVVM,它非常方便!
<ContentControl MouseDoubleClick="OnDoubleClick">
<Border Margin="10" BorderBrush="Black" BorderThickness="2">
<Grid Margin="4">
<Rectangle Fill="Red" />
<TextBlock Text="Hello" FontSize="15" />
</Grid>
</Border>
</ContentControl>