2017-05-11 73 views
0

我已經設定在 this other post工具提示在畫布元素

我做到了以下@ grek40的指示和它的作品很好的描述繪製在畫布元素的應用程序。

現在我想添加一個工具提示到畫布中繪製的元素(只有任務元素由Border類表示)。這是我的代碼:

<DataTemplate DataType="{x:Type local:TaskItem}"> 
    <Border 
    Background="#0074D9" 
    BorderBrush="#001F3F" 
    BorderThickness="2" 
    Width="{Binding Width}" 
    Height="{Binding Height}"> 
     <TextBlock 
     Text="{Binding Id}" 
     FontSize="20" 
     Foreground="White" 
     HorizontalAlignment="Center" VerticalAlignment="Center"/> 
     <Border.ToolTip> 
      <ToolTip Background="#D5F0F0FF"> 
       <StackPanel> 
        <TextBlock Text="Reading Suggestion" FontWeight="Bold"/> 
        <TextBlock Text="Charles Dickens" Margin="5"/> 
       </StackPanel> 
      </ToolTip> 
     </Border.ToolTip> 
    </Border> 
</DataTemplate> 

但它不工作。當我懸停任務元素(邊框項目)時,什麼都不會發生。任何想法有什麼不對?

UPDATE

的網格(x:名稱= 「GRID1」)被包含在一個處理縮放和填充自定義控制。我實施了以下this post in codeplex(我遵循'簡單示例代碼')。

所以我的XAML看起來像這樣:

<ScrollViewer 
    x:Name="scroller" 
    CanContentScroll="True" 
    VerticalScrollBarVisibility="Visible" 
    HorizontalScrollBarVisibility="Visible" 
    > 

    <!-- 
    This is the control that handles zooming and panning. 
    --> 
    <ZoomAndPan:ZoomAndPanControl     
     x:Name="zoomAndPanControl" 
     Background="LightGray" 
     MouseDown="zoomAndPanControl_MouseDown" 
     MouseUp="zoomAndPanControl_MouseUp" 
     MouseMove="zoomAndPanControl_MouseMove" 
     MouseWheel="zoomAndPanControl_MouseWheel" 
     > 

     <!-- 
     This Canvas is the content that is displayed by the ZoomAndPanControl. 
     Width and Height determine the size of the content. 
     --> 
     <Grid x:Name="grid1"> 
      <ItemsControl x:Name="content" 
       ItemsSource="{Binding TaskItems}" 
       ItemContainerStyle="{StaticResource canvasTaskItemStyle}"> 
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <Canvas 
          Background="White" 
          Height="2000" 
          Width="2000"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
      <ItemsControl x:Name="contentLines" 
       ItemsSource="{Binding TaskLineItems}">      
       <ItemsControl.ItemsPanel> 
        <ItemsPanelTemplate> 
         <Canvas 
          Background="Transparent" 
          Height="2000" 
          Width="2000"/> 
        </ItemsPanelTemplate> 
       </ItemsControl.ItemsPanel> 
      </ItemsControl> 
     </Grid> 
    </ZoomAndPan:ZoomAndPanControl> 

</ScrollViewer> 

是也許ZoomAndPanControl與提示干擾的命令?

+0

你確定嗎?我只是將工具提示合併到我的示例中,並且顯示沒有問題。也許你的項目的其他部分正在干擾? – grek40

+0

這是可能的,因爲我有你的代碼在一個自定義控件,處理縮放和paning。我已經更新了有關它的信息。謝謝 – chincheta73

回答

0

您正在網格內疊加兩個不同的itemcontrols。最頂端的控件管理這些行,它具有Background="Transparent",因此它將捕獲所有鼠標流量,並且基礎任務項集合永遠不會收到鼠標移過。

解決方案1(hacky):交換項目和行的訂單或Z-Index。

溶液2(也哈克):除去,而不是使所述阻擋背景它透明(Background="{x:Null}"

溶液3(以下哈克):創建合併的項目集合,其中包含兩個,在數據上下文行和taskitems並使用一個畫布來呈現它們。實施解決方案3的

的一種方法在後面的代碼相結合的項目,另一個是使用CompositeCollection

<ItemsControl ...> 
    <ItemsControl.ItemsSource> 
     <CompositeCollection> 
      <CollectionContainer Collection="{Binding TaskItems}"/> 
      <CollectionContainer Collection="{Binding TaskLineItems}"/> 
     </CompositeCollection> 
    </ItemsControl.ItemsSource> 
    ... 
</ItemsControl> 
+0

謝謝@ grek40我做了解決方案3,並且工作:) – chincheta73

+0

@ chincheta73沒問題,請參閱我的編輯建議如何創建組合集合沒有太大的影響。 – grek40

0

從鏈接帖子中接受的答案,它看起來像你使用這個模板的ItemsControl項目。 ItemsControl將使用ItemContainerStyle屬性定義的容器樣式將每個項目放入容器中。

嘗試將工具提示放在容器上而不是項目中,方法是將其添加到ItemContainerStyle。在另一篇文章中,這是canvasTaskItemStyle

<Style x:Key="canvasTaskItemStyle" TargetType="ContentPresenter"> 
    <Setter Property="Canvas.Left" Value="{Binding CoordX}"/> 
    <Setter Property="Canvas.Top" Value="{Binding CoordY}"/> 
    <Setter Property="ToolTip"> 
     <Setter.Value> 
      <ToolTip Background="#D5F0F0FF"> 
       <StackPanel> 
        <TextBlock Text="Reading Suggestion" FontWeight="Bold"/> 
        <TextBlock Text="Charles Dickens" Margin="5"/> 
       </StackPanel> 
      </ToolTip> 
     </Setter.Value> 
    </Setter> 
</Style> 
+0

仍然不起作用。也許,正如@ grek40項目的另一部分所建議的那樣,這是干擾的。我會用更多的代碼更新我的問題 – chincheta73