【问题标题】:WPF Rectangle does not have a Click eventWPF Rectangle 没有 Click 事件
【发布时间】:2009-07-01 12:46:30
【问题描述】:

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

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

【问题讨论】:

    标签: wpf events shapes


    【解决方案1】:

    如果您对MouseDownMouseUp 不满意,也许您可​​以将Rectangle 放入Button 并处理ButtonClick 事件?

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

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

    【讨论】:

    • 这大概就是我想要的吧。
    【解决方案2】:

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

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

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

    【讨论】:

    【解决方案3】:

    我在寻找相关的 DoubleClick 事件时偶然发现 this suggestion 只是将感兴趣的对象嵌入到 ContentControl 中。

    这是他们的示例(带有边框,也不支持单击/双击)。

    <ContentControl MouseDoubleClick="OnDoubleClick">
        <Border Margin="10" BorderBrush="Black" BorderThickness="2">
            <Grid Margin="4">
                <Rectangle Fill="Red" />
                <TextBlock Text="Hello" FontSize="15" />
            </Grid>
        </Border>
    </ContentControl>
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-01-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多