WPF ListView DoubleClick OriginalSource问题

时间:2010-02-15 13:30:41

标签: wpf listview click double-click listviewitem

我将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作为原始来源,直接我得到了ListViewItem

1 个答案:

答案 0 :(得分:0)

你可以使用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;
}