2014-06-10 2 views
0

J'ai ajouté un bouton à un InkCanvas à l'aide de code-behind. Pour des raisons que je ne comprends pas,Hit test sur enfant de InkCanvas

HitTestResult result = VisualTreeHelper.HitTest(pe.InkCanvas.Children[2], point_MouseDown); 

où le bouton a l'index de 2, résulte toujours en résultat étant nul lorsque je clique sur le bouton.

Quelqu'un sait-il comment déterminer si un élément enfant a été cliqué?

Toute aide sera appréciée. (Oui, j'ai cherché sur Internet sans succès, je m'excuse si cela semble une question stupide).

Système: Windows 7, .net4.0 WPF C#

Edit: Si je fais la même chose où les enfants [0] est un RichTextBox, le plus haut HitTest resturns non nulle. Quelqu'un avec une idée de pourquoi?

Edit: Les deux RichTextBox et le bouton sont ajoutés dans le code-behind, le XAML est:

<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

pourriez-vous partager votre arbre xaml? afin que nous puissions jeter un coup d'œil et essayer de simuler. – pushpraj

Répondre

0

Cela fonctionne, mais il semble compliqué et je ne sais pas pourquoi un bouton apparaît comme 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); 

Ensuite, dans le PreviewMouseLeftButtonDown() (code légèrement modifié à partir du site de Microsoft) ...............

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

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

y at-il une meilleure façon? Et pourquoi le bouton apparaît-il en tant que TextBlock?