2014-12-03 2 views
0

캔버스에서 사용자가 선택한 여러 위치를 나타내는 Rectangles 숫자를 그린 Canvas이 있습니다.사각형 그룹에 대한 사용자 정의 툴팁 표시 (그래프상의 점)

직사각형의 x 및 y 좌표와 다른 점까지의 거리를 나타내는 직사각형 각각에 대해 ToolTip을 작성하려고합니다. "스타일러스 점".

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

'캔버스에 여러 직사각형을 그렸습니다.'- 모든 것을 삭제하고 ItemsControl과 함께 적절한 데이터 바인딩을 사용하십시오. –

+0

@HighCore 감사합니다. 저는 ItemsControl을 사용하는 해결책이 있다고 생각합니다. 이제 답변을 추가 할 것입니다. –

답변

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... 

}