简体   繁体   English

如何从WPF工具提示中生孩子

[英]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. 我想获取名称为asn的文本块,以从中获取文本属性。

Is this possible ? 这可能吗 ?

Edit 1 : if i want to use binding for the textblock with the name :asn 编辑1:如果我想对名称为:asn的文本块使用绑定

to an image source 到图像源

ie : show the image source property in the asn textblock 即:在asn文本块中显示image source属性

(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. 您需要遍历PlacementTarget来找到TextBlock,因为工具提示与TextBlock不在同一个可视树中。

very similar: RelativeSource binding from a ToolTip or ContextMenu 非常相似: 来自ToolTip或ContextMenu的RelativeSource绑定

Could you not name your Image control with x:Name and then bind to it? 您能否不使用x:Name命名Image控件然后绑定到它?

eg 例如

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

ok solved this thanks to @John Gardner 好了,感谢@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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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