WPF에서 예기치 않은 동작이 발생합니다. 여기 내 시나리오는 다음과 같습니다.PreviewLeftMouseButtonDown Listener 다른 이벤트에 대한 MouseButtonEventArgs.Source보고
- 저는 Window에 UserControl을 가지고 있습니다. "표면"이라고 부르 자. 그것에 캔버스가 있습니다.
- 두 번째 UserControl이 있습니다. "PlayingCard"라고합시다.
- PlayingCard UserControl을 Surface의 캔버스의 하위 요소로 추가했습니다. 시각적으로 표면과 꼭대기에 표시됩니다.
이것은 모든 "표면"창 사이에서 일부 끌어서 놓기 작업을위한 설정입니다. Surface 클래스의 모든 드래그 앤 드롭 이벤트를 개별적으로 연결하면 모든 것이 잘 동작합니다. 마우스 오른쪽 아래에있는 PlayingCard UserControl을 감지하고 드래그하여 피드백을 위해 Adorners를 사용할 수 있습니다.
그런 다음 드래그 앤 드롭 방식의 내장을 별도의 고정 클래스로 추상화했습니다. 갑자기, 마우스 클릭 아래에서 감지 된 UIElement는 더 이상 PlayingCard가 아니라 Surface!
현재 Surface의 PreviewLeftMouseButtonDown 이벤트에 연결된 3 개의 다른 이벤트 처리기가 있으며 Surface의 OnPreviewLeftMouseButtonDown 메서드를 재정의하고 있습니다. 이 방법을 처리하는 모든 방법간에 일관된 동작이 기대되지만, 나는 그것을 얻지 못하고 있습니다. 결과는 다음과 같습니다.
(1) Test라는 인스턴스화 된 클래스가 있습니다.이 인스턴스에는 Surface의 PreviewLeftMouseButtonDown 이벤트에 연결된 메서드가 있습니다. 이것은 Window의 main 메소드에서 발생합니다. PlayingCard가 아닌 Surface를 소스로 탐지합니다.
public partial class Window1 : Window
{
public Window1()
{
InitializeComponent();
TestClass test = new TestClass();
_leftSurface.PreviewMouseLeftButtonDown += test.Test_PreviewMouseLeftButtonDown;
(2) I는 메인 윈도우에있어서뿐만 아니라, 표면의 이벤트까지 배선되어 DragDropManager 불리는 정적 클래스가있다. PlayingCard가 아니라 Source로 서피스를 감지합니다.
public Window1()
{
InitializeComponent();
TestClass test = new TestClass();
_leftSurface.PreviewMouseLeftButtonDown += test.Test_PreviewMouseLeftButtonDown;
_leftSurface.PreviewMouseMove += DragDropManager.PreviewMouseMove;
(3) Surface의 OnPreviewLeftMouseButtonDown 이벤트를 오버라이드하고 있습니다. 그것은 소스로 표면을 감지합니다
protected override void OnPreviewMouseLeftButtonDown(MouseButtonEventArgs e)
{
base.OnPreviewMouseLeftButtonDown(e);
(4) 표면 클래스는 XML에 지정된 자신의 이벤트 핸들러를 가지고 있으며, 제대로 소스로 플레잉 카드를 감지합니다.
<UserControl x:Class="WpfTest.Surface"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="300" Width="300">
<Border Name="_border" BorderThickness="4" BorderBrush="Blue">
<Canvas Name="_surface" Background="Black" AllowDrop="True"
ClipToBounds="True"
PreviewMouseMove="Surface_PreviewMouseMove"
PreviewMouseLeftButtonDown="Surface_PreviewMouseLeftButtonDown"
DragOver="Canvas_DragOver" DragLeave="Canvas_DragLeave">
</Canvas>
</Border>
</UserControl>
MouseButtonEventArgs.Source가 XML을 통해 표면에 할당 된 이벤트 핸들러로 올바르게 플레잉 카드를 감지하는 유일한 이벤트 핸들러.
왜 이렇게 다른 결과가 나옵니까? 다른 이벤트 처리기가 PlayingCard를 소스로 올바르게보고하지 않는 이유는 무엇입니까?
짧은 답변이나 직장 네트워크 시간이 즉시 필요합니다. – Donnelle
OriginalSource가 나에게 Canvas on the Surface를 제공합니다. 도움이 안돼. –