我有这样的代码
<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文本块中显示图像源属性
(文本块放在具有图像子项的自定义控件上)
<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>
答案 0 :(得分:1)
您需要通过PlacementTarget
查找TextBlock,因为工具提示与TextBlock不在同一个可视树中。
答案 1 :(得分:0)
你能用x:Name命名你的Image控件,然后绑定它吗?
e.g。
<Image x:Name="MyImage" Source="Stuff"/>
<TextBlock Text="{Binding ElementName=MyImage, Path=Source}"/>
答案 2 :(得分:0)
好的,这要归功于@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