2014-06-10 5 views
0

코드 숨김을 통해 InkCanvas에 버튼을 추가했습니다. 이해할 수없는 이유 때문에 버튼을 클릭하면 버튼에 2의 인덱스가있는 결과가 항상 null이됩니다.InkCanvas의 자식에 대한 히트 테스트

아무도 자식 요소가 클릭되었는지 확인하는 방법을 알고 있습니까?

도움이 될 것입니다. (네, 저는 인터넷을 성공하지 않고 검색했습니다. 이것이 바보 같은 질문이라면 사과드립니다.)

체제 : Windows 7, WPF C 번호를

편집을 .net4.0 : 나는 아이 [0] 그러나 hitTest 위가 null resturns 다음를 RichTextBox이며, 같은 일을합니다. 왜 그런 생각을 가진 사람?

편집 다음를 RichTextBox과 버튼 모두 코드 숨김에서 추가의 XAML은 다음과 같습니다

<Grid Height="{x:Static local:pe.heightCanvas}" > 

      <!--NotepadCanvas. Canvas used to place user writing lines and borders.--> 
      <Canvas Name="NotePadCanvas" Panel.ZIndex="0" 
        Width="{x:Static local:pe.widthCanvas}" 
        Height="{x:Static local:pe.heightCanvas}" 
        Background="{Binding documenttype, Converter={StaticResource inkCanvasBackgroundConverter}}" /> 

      <!--BackgroundCanvas. Canvas used for special highlighting of seleted items--> 
      <Canvas Name="BackgroundCanvas" Panel.ZIndex="1" 
        Width="{x:Static local:pe.widthCanvas}" 
        Height="{x:Static local:pe.heightCanvas}" 
        Background="Transparent" /> 

      <!--FormsCanvas. Canvas used to place formatted text from lists, etc.--> 
      <Canvas Name="FormsCanvas" Panel.ZIndex="2" 
        Width="{x:Static local:pe.widthCanvas}" 
        Height="{x:Static local:pe.heightCanvas}" 
        Background="Transparent" /> 

      <!--TranscriptionCanvas. Canvas used to place recognized ink from the InkAnalyzer--> 
      <Canvas Name="TranscriptionCanvas" Panel.ZIndex="3" 
        Width="{x:Static local:pe.widthCanvas}" 
        Height="{x:Static local:pe.heightCanvas}" 
        Background="Transparent" /> 

      <!--InkCanv. Top most canvas used to gather handwritten ink strokes from the user. EditingMode="Ink" Gesture="OnGesture" --> 
      <local:CustomInkCanvas x:Name="InkCanvas" Panel.ZIndex="4" 
         Width="{x:Static local:pe.widthCanvas}" 
         Height ="{x:Static local:pe.heightCanvas}" 
         Background="Transparent" 
         AllowDrop="True" Drop="InkCanvas_Drop"/> 

     </Grid> 
+0

xaml 트리를 공유 할 수 있습니까? 그래서 우리는 모양을 가지고 시뮬레이션하려고 시도 할 것입니다. – pushpraj

답변

0

이 작동하지만 뒤얽힌 보인다 및 버튼이있는 TextBlock로 나타 왜 몰라 :

........... 

    Button btn = new Button(); 
     btn.Name = "Cancel"; 
     btn.Content = "Cancel"; 
     btn.Background = Brushes.Red; 
     btn.Width = 50; 
     btn.Height = 25; 
     btn.RenderTransform = new System.Windows.Media.TranslateTransform(pe.InkCanvas.ActualWidth-btn.Width,topmargin); 

     btnCancel = btn; 
     pe.InkCanvas.Children.Add(btnCancel); 

다음, PreviewMouseLeftButtonDown()에서 ............... (코드는 마이크로 소프트의 사이트에서 약간 수정)

// Clear the contents of the list used for hit test results. 
       hitResultsList.Clear(); 


       // Set up a callback to receive the hit test result enumeration. 
       VisualTreeHelper.HitTest(pe.InkCanvas, null, 
        new HitTestResultCallback(MyHitTestResult),      new PointHitTestParameters(point_MouseDown)); 

       for (int i = 0; i < hitResultsList.Count; i++) 
       { 
        TextBlock tb = hitResultsList[i] as TextBlock; 
        if (tb == null) continue; 
        if (tb.Text == "Cancel") 
        { 
         Cancel_Click(); 
        } 
       } 

............................................... ...

List<DependencyObject> hitResultsList = new List<DependencyObject>(); 

    // Return the result of the hit test to the callback. 
    public HitTestResultBehavior MyHitTestResult(HitTestResult result) 
    { 
     // Add the hit test result to the list that will be processed after the enumeration. 
     hitResultsList.Add(result.VisualHit); 

     // Set the behavior to return visuals at all z-order levels. 
     return HitTestResultBehavior.Continue; 
    } 

는 더 좋은 방법이 있나요? 왜 버튼이 TextBlock으로 표시됩니까?