2014-06-10 75 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?