2012-02-01 1 views
0

나는이 같은 몇 가지 이벤트를 무시 UserControl이있는 UserControl을 위해 그리드로부터 발생하지 :WPF는, 어떤 이벤트가

public class MyControl: UserControl 
{ 
    protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e) 
    { 
     base.OnMouseLeftButtonUp(e); 
     // handle mouse event up 
    } 
} 

내가 antother UserControl을이 컨트롤을 추가를 -> 그리드,이 그리드 이는 MouseUp 등록했다.

public class MyParent: UserControl 
{ 
    private void Grid_MouseUp(object sender, MouseButtonEventArgs e) 
    { 
      // handle grid mouse event 
    } 
} 

그리고 MyParent XAML에서

단순히 있습니다

<UserControl ... > 
     <Grid Name="Grid" MouseUp="Grid_MouseUp"> 
       <my:MyControl Height="250" Width="310" Visibility="Visible" Opacity="1" IsEnabled="True" /> 
     </Grid> 
</UserControl> 

내가 뭘 알 것은 내가 MyControl 행사를 통해 마우스를 놓을 때 그리드에 의해 캡처 및 MyControl로 연결되지 않는 것입니다, 왜?

MyControl 클래스 내에서 MouseUp 이벤트를 수신하려면 어떻게해야합니까?

편집 MouseDown와 예상대로, 단지 작품 ... 두 이벤트가 너무 차이가 무엇인가도 상위 그리드에 등록되어 있지 이는 MouseUp 모든 작품? EDIT2

확인

, 난 내가 MyParent에 MyControl를 추가하면 내가 문제를 찾은 것 같아 -> 바로 XAML에서 그리드, 모두가 잘 작동하지만, 내가 프로그래밍 "MyParentInstance.Grid.Children을 추가합니다. Add (MyControlInstance) "그런 다음 위의 문제가 있습니다.

컨트롤을 추가하는 코드가 맞습니까?

감사합니다.

답변

0

RoutedEvent은 특정 이벤트를 호출하는 요소의 부모에서만 작동합니다. 즉, 그리드의 모든 상위 컨트롤이 이벤트를 수신하지만 자녀는 표시되지 않습니다. 라우팅 이벤트에 대한 자세한 내용은 Routed Events Overview을 참조하십시오. 문제를 해결하기 위해 나는 MyControl에서 MouseUp 이벤트를 등록하는 제안 :

<UserControl ... > 
    <Grid Name="Grid"> 
    <my:MyControl MouseUp="Grid_MouseUp" Height="250" Width="310" Visibility="Visible" Opacity="1" IsEnabled="True" /> 
    </Grid> 
</UserControl> 
+0

그래 난 알아,하지만 MyControl은 프로그램 추가, 그것은 MyParent 자체에 의해 처리되지 않는, 그래서 내가 할 수있는 MyUparent에 직접 MouseUp을 등록하지 마십시오. – user1182622

0

내가 포럼의 광범위한 검색 후이 질문에 대한 답변 건너하지 않은이 게시물 있지만 여기 내 솔루션을 제공하고 있습니다 있도록 일자. 나는 이미 자식 컨트롤에 public으로 만들어 놓은 이벤트 핸들러를 사용했습니다. ChildControl 사용자 정의 컨트롤이 최상위 창에 추가되면 이러한 컨트롤이 실행됩니다. 부모 내에서 중첩 된 경우 이벤트는 부모 수준에서 처리되어야합니다. 프로그래밍 방식으로 컨트롤을 추가 할 때

, 같은 부모 컨트롤에 이벤트 처리기를 추가 :

class Parent : UIElement 
{ 
    //... 

    void Parent_AddChildControl() 
    { 
     ChildControl child = new ChildControl(); 
     child.MouseEnter += child_MouseEnter; 
     UiElementX.Children.Add(child); 
    } 

    void child_MouseEnter(object sender , MouseEventArgs e) 
    { 
     ((ChildControl)sender).ChildControl_MouseEnter(sender, e); 
    } 
} 

class ChildControl : UIElement 
{ 
    //... 

    public void ChildControl_MouseEnter(object sender, MouseEventArgs e) 
    { 
     //event handling 
    } 
}