2016-07-11 1 views
2

다른 컨트롤에 새로운 이벤트로 인상 :WPF 엄지 손가락을 두 번 클릭 이벤트를 처리하고 난 행운</p> <p>행동으로 행동을 시도

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(); 
    } 
} 

XAML :

<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

첫째, 그것도'AssociatedObject_PreviewMouseDown'의 이벤트 핸들러를 실행 않습니다 NT OnPreviewMouseDoubleClick 그래서 나는이 같은 이벤트를 관리 할 수있는 인스턴스 처리기를 추가했다? –

+0

@ MarkusHütter 예! – sam

답변

1

위의 코드는 정상적으로 작동하지만 대상 컨트롤이 전날을받지 못했습니다. 모든

this.AddHandler(Control.PreviewMouseDoubleClickEvent, 
    new MouseButtonEventHandler((target, args) => 
    { 
     //do stuff 
     args.Handled = true; 
    }), false);