[英]How do I get a child from WPF tooltip
我有这样的代码
<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"/>
我想获取名称为asn的文本块,以从中获取文本属性。
这可能吗 ?
编辑1:如果我想对名称为:asn的文本块使用绑定
到图像源
即:在asn文本块中显示image source属性
(文本块放置在具有图像子级的自定义控件上)
<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>
您需要遍历PlacementTarget
来找到TextBlock,因为工具提示与TextBlock不在同一个可视树中。
您能否不使用x:Name命名Image控件然后绑定到它?
例如
<Image x:Name="MyImage" Source="Stuff"/>
<TextBlock Text="{Binding ElementName=MyImage, Path=Source}"/>
好了,感谢@John Gardner解决了这个问题
解决方案很简单
<TextBlock FontSize="18" Foreground="DarkGreen" Text="{Binding IsAsync=True, Path=PlacementTarget,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ToolTip},Converter={StaticResource myconvertortool}}"/>
这会将自定义控件实例传递给转换器,该转换器仅从中获取图像
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.