简体   繁体   中英

How do I get a child from WPF tooltip

I have code like this

<Setter Property="ToolTip" >
   <Setter.Value>
      <ToolTip>
         <StackPanel>
            <TextBlock Text="Assignment Name : " FontSize="18">
            <TextBlock Name="asn" FontSize="18" Foreground="GreenYellow" Text="nothing here"/> 
            </TextBlock>
         </StackPanel>
      </ToolTip>
   </Setter.Value>
</Setter>
<EventSetter Event="ToolTipOpening" Handler="ToolTip_Opening"/>

I want to get the textblock with the name asn , To get text property from it.

Is this possible ?

Edit 1 : if i want to use binding for the textblock with the name :asn

to an image source

ie : show the image source property in the asn textblock

(text block is placed on a custom control that has an image child)

<Style TargetType="Controls:Tile">
     ...
        <Setter Property="ToolTip" >
         <Setter.Value>
           <ToolTip>
               <StackPanel> 
                   <TextBlock Text="Assignment Name : " FontSize="18">       
                         <TextBlock Name="asn" FontSize="18" Foreground="GreenYellow" Text="nothing here"/> 
                    </TextBlock>
                </StackPanel>
           </ToolTip>
        </Setter.Value>
   </Setter>
    <EventSetter Event="ToolTipOpening" Handler="ToolTip_Opening"/>
  </Style>

You need to go through PlacementTarget to find the TextBlock, because the tooltip isn't in the same visual tree as the TextBlock.

very similar: RelativeSource binding from a ToolTip or ContextMenu

Could you not name your Image control with x:Name and then bind to it?

eg

<Image x:Name="MyImage" Source="Stuff"/>
<TextBlock Text="{Binding ElementName=MyImage, Path=Source}"/>

ok solved this thanks to @John Gardner

solution is pretty simple

<TextBlock  FontSize="18" Foreground="DarkGreen" Text="{Binding IsAsync=True, Path=PlacementTarget,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ToolTip},Converter={StaticResource myconvertortool}}"/>

this will pass the custom control instance to the converter which simply gets the image from it

   Public Class ToolTipToSourceConverter
    Implements IValueConverter

    Public Function Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
        If value Is Nothing Then
            Return "Error "
        End If
        Try
            Dim sndr = DirectCast(value, MahApps.Metro.Controls.Tile)
            Dim sndrimage = DirectCast(sndr.GetChildObjects(False)(0), Image)
            Dim imgname As String = sndrimage.Source.ToString.Substring(sndrimage.Source.ToString.LastIndexOf("/"c) + 1)


            Return StatsDict.InquireForAssignment(imgname).Name.ToString.Replace("_", " ")
        Catch ex As Exception
            Return value.GetType.ToString
        End Try
        'if is connected to master True then hide
    End Function

    Public Function ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
        Throw New NotImplementedException
    End Function
End Class

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM