处理鼠标点击/双击/ mousedown并提升到孩子

时间:2016-07-08 13:51:50

标签: wpf listbox listboxitem

我在ListBoxItem中使用Thumb来在Canvas周围移动项目。 拇指覆盖ListBoxItem的整个表面。 由于ListBoxItem可以包含例如TextBox,我想拦截双击(目前被Thumb吞并)并将它们直接转发到Texbox。

我尝试了一个没有运气的行为(targetElement没有收到这个事件)......这是代码,有什么帮助吗?

public sealed class BubbleDoubleClickEvent : Behavior<UIElement>
{
    #region TargetElement
    public UIElement TargetElement
    {
        get { return (UIElement)GetValue( TargetElementProperty ); }
        set { SetValue( TargetElementProperty, value ); }
    }

    // Using a DependencyProperty as the backing store for TargetElement.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty TargetElementProperty =
        DependencyProperty.Register( "TargetElement", typeof( UIElement ), typeof( BubbleDoubleClickEvent ) );
    #endregion

    protected override void OnAttached()
    {
        base.OnAttached();
        AssociatedObject.PreviewMouseDown += AssociatedObject_PreviewMouseDown;
    }

    private void AssociatedObject_PreviewMouseDown( object sender, MouseButtonEventArgs e )
    {
        if( e.ClickCount == 2 )
        {
            e.Handled = true;
            var e2 = new MouseButtonEventArgs( e.MouseDevice, e.Timestamp, e.ChangedButton );
            e2.RoutedEvent = Control.PreviewMouseDoubleClickEvent;

            var target = TargetElement ?? AssociatedObject;
            target.RaiseEvent( e2 );
        }
    }

    protected override void OnDetaching()
    {
        AssociatedObject.PreviewMouseDown -= AssociatedObject_PreviewMouseDown;
        base.OnDetaching();
    }
}

Listbox定义ItemContainerStyle模板,类似于:

<Grid>
   <ContentPresenter x:Name="contentPresenter" Content="{Binding}"    ContentTemplate="{Binding ..., Path=ItemTemplate }" />

   <Thumb x:Name="moveThumb" >
        <i:Interaction.Behaviors>
            <behaviors:BubbleDoubleClickEvent TargetElement="{Binding   ElementName=contentPresenter}"/>
        </i:Interaction.Behaviors>
   </Thumb>
</Grid>

0 个答案:

没有答案