Sakuya
Sakuya

Reputation: 680

Value from Binding ElementName producing empty string

I have a Datagrid which has a DataGridTemplateColumn containing a ListView whose Itemsource is bound to an array of string.

Within that ListView I have defined an ItemTemplate as I want the Foreground of each item in the list to be dependant on condition - so that's applied to a TextBlock.

I want a tooltip to display when each item in the ListView is hovered over, so I have a Tooltip defined in that TextBlock

What I'm trying to do is have that Tooltip display the title/detail about a specific item by using a converter (to get the index of the item in a different list).

For this I need the Tooltip to know the ListView item but I can't seem to get that to work. The TextBlock itself retrieves it using Path=., I have tried naming the TextBlock ListItem and retrieving it as ElementName to no avail - the result is just empty string.

Here's the relevant xaml (with formatting properties removed).

<ListView.ItemTemplate>
    <DataTemplate>
        <TextBlock x:Name="ListItem" Text="{Binding Path=.}" Foreground="{Binding RelativeSource={RelativeSource Self}, Path=Text, Converter={StaticResource ConvertItemToColour}}">
            <TextBlock.ToolTip>
                <ToolTip>
                    <StackPanel Orientation="Vertical">
                        <Label>
                            <TextBlock Text="{Binding ElementName=ListItem, Path=Text, Converter={StaticResource ConvertItemToTitle}}"/> <!-- Item Title -->
                        </Label>
                        <Label>
                            <TextBlock/> <!-- Item Description -->
                        </Label>
                    </StackPanel>
                </ToolTip>
            </TextBlock.ToolTip>
        </TextBlock>
    </DataTemplate>
</ListView.ItemTemplate>

Upvotes: 2

Views: 178

Answers (1)

dkozl
dkozl

Reputation: 33384

ToolTip, like Popup, is not part of the main visual tree so neither ElementName nor RelativeSource binding will work beyond ToolTip but DataContext inside ToolTip should still be the same as for ListViewItem

<ToolTip>
   <StackPanel Orientation="Vertical">
      <Label>
         <TextBlock Text="{Binding Path=., Converter={StaticResource ConvertItemToTitle}}"/>
      </Label>
   </StackPanel>
</ToolTip>

Text="{Binding Path=.}" should give you same result inside ToolTip as in DataTemplate

Upvotes: 1

Related Questions