2014-11-08 3 views
1

아래 코드는 내 수수께끼의 완전한 예입니다. 프론트 피어에서 마우스 이벤트가 작동하는 동안 마우스 입력/나가기 이벤트가 다시 피어에서 작동해야합니다. 지금까지 나는 단지 하나 또는 다른 것을 얻을 수 있습니다. 전면 그리드의 배경을 제거하여 배경 이벤트를 얻습니다. 그런 다음 클릭 이벤트가 작동하지 않습니다. 이 작업에서 모든 사건을 어떻게 만들 수 있습니까? 실제 응용 프로그램이이 예제보다 훨씬 복잡하기 때문에 논리적/시각적 구조를 변경하고 싶지 않습니다.WPF : 앞쪽에서 마우스를 클릭하는 동안 백 피어에서 마우스를 입력/나가기

using System; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Shapes; 

namespace DemoBadEnterEvent 
{ 
    class Program 
    { 
     [STAThread] 
     public static void Main() 
     { 
      var frontGrid = new Grid 
      { 
       Background = Brushes.Transparent // remove this for enter/leave to work 
      }; 

      var backGrid = new Grid(); 
      var backEllipse = new Ellipse 
      { 
       HorizontalAlignment = HorizontalAlignment.Center, 
       VerticalAlignment = VerticalAlignment.Center, 
       Width = 200, 
       Height = 200, 
       Fill = Brushes.LightSteelBlue, 
       StrokeThickness = 10, 
       Stroke = Brushes.Transparent, 
       Cursor = Cursors.Arrow 
      }; 

      var window = new Window { Width = 300, Height = 300, Cursor = Cursors.Cross }; 
      var app = new Application(); 
      backGrid.Children.Add(backEllipse); 
      backGrid.Children.Add(frontGrid); 
      window.Content = backGrid; 

      backEllipse.MouseEnter += (sender, args) => backEllipse.Fill = Brushes.MediumVioletRed; 
      backEllipse.MouseLeave += (sender, args) => backEllipse.Fill = Brushes.LightSteelBlue; 
      frontGrid.MouseLeftButtonDown += (sender, args) => backEllipse.Stroke = Brushes.Salmon; 
      frontGrid.MouseLeftButtonUp += (sender, args) => backEllipse.Stroke = Brushes.Transparent; 

      app.Run(window); 
     } 
    } 
} 
+0

참고 : 나는 IsMouseOver 속성에 바인딩을 시도하지만 MouseEnter에 아무 상관이 없습니다. – Brannon

답변

0

레이아웃을 변경하지 않고도 backEllipse에서 마우스 이벤트를 가져올 수 없습니다. 당신이 할 수있는 한 가지가 frontGrid에 캐치 마우스를 이동하고 backEllipse에의 히트 테스트를 수행합니다

frontGrid.MouseMove += (sender, args) => backEllipse.Fill = 
    (VisualTreeHelper.HitTest(backEllipse, args.GetPosition(backEllipse)) == null) 
    ? Brushes.LightSteelBlue 
    : Brushes.MediumVioletRed; 
+0

MouseEnter의 동작은 모든 MouseMove의 히트 테스트와 동일합니까? 아니면 네이티브 MouseEnter가 그보다 더 효율적입니까? – Brannon