2014-06-10 78 views
0

我通过代码隐藏的方式向InkCanvas添加了一个按钮。至于原因,我无法理解,InkCanvas的儿童点击测试

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

在按钮具有指数为2,结果总是在结果为空时,我点击了按钮。

有人知道如何确定子元素是否被点击过?

任何帮助将不胜感激。 (是的,我已经搜索了互联网没有成功,我很抱歉,如果这似乎是一个愚蠢的问题)。

系统:Windows 7,.net4.0 WPF C#

编辑:如果我这样做,让孩子[0]是一个RichTextBox,那么上述的HitTest resturns非空同样的事情。任何人有任何想法为什么?

编辑:两个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()(代码略从微软的网站修改) ...............

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

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?