【问题标题】:Why doesn't WPF border control have a mousedoubleclick event?为什么 WPF 边框控件没有 mousedoubleclick 事件?
【发布时间】:2011-03-25 13:22:05
【问题描述】:

为什么WPF边框控件没有mousedoubleclick事件?我有一个 ItemsControl,其中包含一些用于 DataTemplate 的布局内容。我想处理双击事件以弹出一个详细信息对话框,但是我的布局容器的边框似乎没有暴露该事件。

关于如何获得双击事件或重做 xaml 以使其成为可能的任何建议?

【问题讨论】:

    标签: wpf xaml


    【解决方案1】:

    只需使用 InputBindings。

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

    一般;如果不在 WPF 中开发控件,请避免使用事件。通常,使用基于事件的代码是 MVVM 模式中断的强烈指示。

    【讨论】:

    • 我很少在 stackoverflow 上这么快就找到我的问题的完美答案
    • 优雅的解决方案。比定义自己的双击行为要好。
    【解决方案2】:

    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>
    

    【讨论】:

    • 您也可以使用ContentControl 并指定内容而不是模板。
    【解决方案3】:

    更新:对不起,我的错 - 迟到了

    在鼠标按下事件中获取 ClickCount

     //  e.Handled = true;  optional
    
     if (e.ClickCount > 1)
     {
        // here comes double click and more :)
     }
    

    【讨论】:

    • Stackpanel 也没有双击事件。你建议我用它做什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-15
    相关资源
    最近更新 更多