为什么WPF边框控件没有鼠标双击事件?

33

为什么WPF边框控件没有鼠标双击事件?我有一个带有DataTemplate布局的ItemsControl。 我想处理双击事件以弹出详细信息对话框,但是边框,我的布局容器,似乎没有公开该事件。

有没有建议如何获得双击事件,或者重新调整XAML使其成为可能?

3个回答

67

只需使用 InputBindings 即可。

<Border>
    <Border.InputBindings>
        <MouseBinding MouseAction="LeftDoubleClick" Command="..."/>
    </Border.InputBindings>
</Border>

一般情况下,如果不是在开发WPF控件,请避免使用事件。通常,基于代码后台的事件使用是MVVM模式破坏的强烈指示。


4
在stackoverflow上很少能如此快地找到完美的答案来回答我的问题。 - NielW
3
优雅的解决方案。比定义自己的双击行为要好。 - jHilscher

16

MouseDoubleClick在Control上声明,所以您只需要在ItemTemplate中使用某个Control的实例。最简单的方法是使用没有任何其他行为的基本Control类,并使用当前ItemTemplate中的内容提供自定义模板。

<ItemsControl>
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Control MouseDoubleClick="Control_MouseDoubleClick">
                <Control.Template>
                    <ControlTemplate>
                        <Border>
                            <!--Other ItemTemplate stuff-->
                        </Border>
                    </ControlTemplate>
                </Control.Template>
            </Control>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

1
您还可以使用“ContentControl”并指定内容,而不是模板。 - user1618054

7
更新:抱歉,我的错 - 太晚了。
在您的鼠标按下事件中获取 ClickCount。
 //  e.Handled = true;  optional

 if (e.ClickCount > 1)
 {
    // here comes double click and more :)
 }

StackPanel也没有双击事件。你建议我该怎么做? - Mr Bell

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接