2014-12-03 53 views
0

我有一個Canvas,我在其上繪製了多個Rectangles,它們代表畫布上多個用戶選擇的位置。顯示一組矩形的自定義工具提示(圖形上的點)

我想爲每個矩形創建一個ToolTip,該矩形顯示矩形的x和y座標以及到另一個點的距離:「手寫筆點」。

當創建矩形時,x和y座標是已知的,但與觸筆點的距離不是,因此工具提示每次顯示時都需要更新其文本。

我試過使用下面的綁定,但這只是在工具提示中放置文本「System.Windows.Control.ToolTip」。

... 

    Rectangle rectangle = new Rectangle 
    { 
     Width = _rectWidth, 
     Height = _rectWidth, 
     Fill = new SolidColorBrush(Colors.Red) 
    }; 

    rectangle.ToolTip = new ToolTip(); 
    Binding binding = new Binding() 
    { 
     Source = this, 
     Path = new PropertyPath("ToolTipBinding"), 
     Mode = BindingMode.OneWay, 
     UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged 
    }; 

    BindingOperations.SetBinding(rectangle.ToolTip as ToolTip ,ToolTipService.ToolTipProperty, binding); 
} 

public string ToolTipBinding 
{ 
    get 
    { 
     return "How would i get the data context here (even if it bound correctly)"; 
    } 
} 

任何幫助非常感謝。

+0

'我有一個Canvas,我在其上畫了一些Rectangles' - 刪除所有這些,並使用適當的DataBinding和ItemsControl。 – 2014-12-03 12:11:29

+0

@HighCore謝謝,我認爲我有一個與ItemsControl工作解決方案現在將添加答案。 – 2014-12-04 08:54:18

回答

0

這是我想出的解決方案。這將在畫布上顯示一列點作爲正方形。下面的TargetList是包含每個點所需數據的Target對象的列表。希望這可以幫助某人。爲工具提示創建

資源:

<UserControl.Resources> 
    <Style TargetType="{x:Type ToolTip}"> 
     <Setter Property="Template"> 
      <Setter.Value> 
       <ControlTemplate TargetType="{x:Type ToolTip}"> 
        <Grid> 
         <Grid.RowDefinitions> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
          <RowDefinition /> 
         </Grid.RowDefinitions> 
         <Grid.ColumnDefinitions> 
          <ColumnDefinition /> 
          <ColumnDefinition /> 
         </Grid.ColumnDefinitions> 
         <TextBox Grid.Row="0" Grid.Column="0" Text="X:" /> 
         <TextBox Grid.Row="0" Grid.Column="1" Text="{Binding Path=PositionX}" /> 
         <TextBox Grid.Row="1" Grid.Column="0" Text="Z:" /> 
         <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding Path=PositionZ}" /> 
         <TextBox Grid.Row="2" Grid.Column="0" Text="ΔX:" /> 
         <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding Path=DeltaX}" /> 
         <TextBox Grid.Row="3" Grid.Column="0" Text="ΔZ:" /> 
         <TextBox Grid.Row="3" Grid.Column="1" Text="{Binding Path=DeltaZ}" /> 
        </Grid> 
       </ControlTemplate> 
      </Setter.Value> 
     </Setter> 
    </Style> 
<UserControl.Resources> 

項目控制

<ItemsControl Grid.Row="0" Grid.Column="0" ClipToBounds="True" ItemsSource="{Binding TargetList}"> 
    <ItemsControl.ItemsPanel> 
     <ItemsPanelTemplate> 
      <Canvas /> 
     </ItemsPanelTemplate> 
    </ItemsControl.ItemsPanel> 
    <ItemsControl.ItemContainerStyle> 
     <Style TargetType="ContentPresenter"> 
      <Setter Property="Canvas.Left" Value="{Binding Path=PositionScreen.X}" /> 
      <Setter Property="Canvas.Top" Value="{Binding Path=PositionScreen.Z}" /> 
     </Style> 
    </ItemsControl.ItemContainerStyle> 
    <ItemsControl.ItemTemplate> 
     <DataTemplate> 
      <Rectangle Width="{Binding RectangleSize}" 
         Height="{Binding RectangleSize}" 
         Fill="{Binding RectangleBrush}" > 
       <Rectangle.ToolTip> 
        <ToolTip /> 
       </Rectangle.ToolTip> 
      </Rectangle> 
     </DataTemplate> 
    </ItemsControl.ItemTemplate> 
</ItemsControl> 

目標類,只顯示屬性名稱。這些根據需要進行更新。

public class Target : ObservableObject 
{ 
    public Point3D PositionX... 

    public Point3D PositionZ... 

    public double DeltaX... 

    public double DeltaZ... 

    public Point3D PositionScreen... 

    public double RectangleSize... 

    public Brush RectangleBrush... 

} 
相關問題