如何在WPF中显示工具提示值?

时间:2014-07-10 12:58:37

标签: c# wpf xaml tooltip

在WPF中,我创建了一个这样的矩形:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"                    
                    xmlns:s="clr-namespace:DiagramDesigner"
                    xmlns:c="clr-namespace:DiagramDesigner.Controls"
                    x:Class="GeoOvwSample.RectangleGeometryRoundedCornerExample"
                >

<Brush x:Key="ItemStroke">#FFD69436</Brush>
<LinearGradientBrush x:Key="ItemBrush" StartPoint="0,0" EndPoint="0,1">
    <LinearGradientBrush.GradientStops>
        <GradientStop Color="#FAFBE9" Offset="0" />
        <GradientStop Color="Orange" Offset="1" />
    </LinearGradientBrush.GradientStops>
</LinearGradientBrush>

<Brush x:Key="ItemStroke1">#ACADCD</Brush>
<LinearGradientBrush x:Key="ItemBrush1" StartPoint="0,0" EndPoint="0,1" >
    <GradientStop Color="#FEFEFE" Offset="0"/>
    <GradientStop Color="#BDBEDE" Offset="1"/>
</LinearGradientBrush>

<Style x:Key="FlowChartRectangleStyle" TargetType="Rectangle">
    <Setter Property="Fill" Value="{StaticResource ItemBrush}"/>
    <Setter Property="Stroke" Value="{StaticResource ItemStroke}"/>
    <Setter Property="StrokeThickness" Value="1"/>
    <Setter Property="StrokeLineJoin" Value="Round"/>
    <Setter Property="Stretch" Value="Fill"/>
    <Setter Property="IsHitTestVisible" Value="False"/>
    <Setter Property="SnapsToDevicePixels" Value="True"/>    
</Style>

    <Style x:Key="Data" TargetType="Rectangle" BasedOn="{StaticResource FlowChartRectangleStyle}">             
</Style>

<Style x:Key="Data_DragThumb" TargetType="Rectangle" BasedOn="{StaticResource Data}">
    <Setter Property="IsHitTestVisible" Value="true"/>
    <Setter Property="Height" Value="300"/>
    <Setter Property="Width" Value="200"/>        
    <Setter Property="Tag" Value="DataShape" />   
</Style>


<s:Toolbox x:Key="FlowChartStencils" ItemSize="100,90" SnapsToDevicePixels="True"
         ScrollViewer.HorizontalScrollBarVisibility="Disabled">
    <ItemsControl.Items>

   <Rectangle Style="{StaticResource Data}" ToolTip="DataTest" StrokeThickness="2">                                
    <s:DesignerItem.DragThumbTemplate>                    
        <ControlTemplate>                        
            <Rectangle Style="{StaticResource Data_DragThumb}" x:Name="DataShape" Tag="DataShapeTag" />
         </ControlTemplate>
     </s:DesignerItem.DragThumbTemplate>                
    </Rectangle>

    </ItemsControl.Items>

    </s:Toolbox>
</ResourceDictionary>

这会在面板上显示一个矩形,我可以在GUI中选择并拖动它。现在我想在形状上创建一种文本块,以便显示其工具提示值,因此工具提示值与形状一起显示。我试图创建文本块并将其与矩形形状绑定,但不知何故我的代码不正确。怎么做?还是有一个更简单的方法?谢谢。

1 个答案:

答案 0 :(得分:0)

您只需将TextBlock添加到任何容器控件中(但之后)Rectangle元素,然后数据绑定Rectangle.ToolTip的值即可TextBlock.Text属性。尝试这样的事情:

<StackPanel>
    <Rectangle Name="Rectangle" Style="{StaticResource Data}" ToolTip="DataTest" 
        StrokeThickness="2" />
    <TextBlock Text="{Binding ToolTip, ElementName=Rectangle}" />
</StackPanel>