簡體   English   中英

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

[英]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不在同一個可視樹中。

非常相似: 來自ToolTip或ContextMenu的RelativeSource綁定

您能否不使用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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM