【问题标题】:WPF ListView DoubleClick OriginalSource problemWPF ListView DoubleClick OriginalSource 问题
【发布时间】:2010-02-19 16:32:00
【问题描述】:

我将EventHandler 附加到MouseDoubleClick 事件。

<Style TargetType="ListViewItem" BasedOn="{StaticResource MyStyle}">                                
    <EventSetter Event="MouseDoubleClick" Handler="ListViewItem_MouseDoubleClick" />
</Style>

private void ListViewItem_MouseDoubleClick(object sender, RoutedEventArgs e) {}

ListView 的视图基于GridView,其中一列包含CheckBox。 如果CheckBox 被双击,我希望能够忽略双击

问题是 我找不到原始来源 (CheckBox) 来阻止它,就像路由事件一样,我得到了 Theme 作为原始来源,而直接我得到了 @987654329 @。

【问题讨论】:

    标签: wpf listview click double-click listviewitem


    【解决方案1】:

    您可以使用VisualTreeHelper 来确定 OriginalSource 的任何祖先是否是这样的 CheckBox:

    private void ListViewItem_MouseDoubleClick(object sender, RoutedEventArgs e)
    {
        var obj = e.OriginalSource as DependencyObject;
    
        // suppress event?
        if (IsWithinCheckBox(obj))
            return;
    
        // handle your event here
    }
    
    private bool IsWithinCheckBox(DependencyObject obj)
    {
        while (obj != null)
        {
            if (obj is CheckBox)
                return true;
    
            obj = VisualTreeHelper.GetParent(obj);
        }
    
        return false;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-04-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-25
      • 1970-01-01
      • 2011-02-19
      • 2011-01-17
      相关资源
      最近更新 更多