2

I want binding Text in Tooltip but i have one problem, it is binding value is other element controls, therefore i cannot basically get their value through binding.

<TextBlock x:Name="txb2" Text="Hello Stackoverflow"/>

<TextBox Grid.Row="1" TextChanged="TextBox_TextChanged">
    <TextBox.ToolTip>
        <TextBlock>
            <Run Text="{Binding ElementName=txb2, Path=Text}" FontWeight="Bold"/>
        </TextBlock>
    </TextBox.ToolTip>
</TextBox>

basically I tried binding this code.

ijaewung
  • 53
  • 1
  • 8
  • You have a typo, you're binding to `Txb2` but the element name is `txb2`. – Loocid Nov 15 '18 at 03:40
  • WPF/UWP and XAML were designed with MVVM in mind. While you can use other approaches, doing so misses about 90% of it's power and runs into issues at every other point. I can not tell if this is MVVM, but I tend towards asuming it is not. In MVVM wich Element(s) represent any ViewModel value is a minor detail. – Christopher Nov 15 '18 at 03:42
  • Thank you Christopher :) your advice is very important message. but i just want fixing answer in this case, that is not MVVM pattern base. – ijaewung Nov 15 '18 at 03:45

2 Answers2

3

If you look at the output you will see an error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=txb2'. BindingExpression:Path=Text; DataItem=null; target element is 'Run' (HashCode=58577354); target property is 'Text' (type 'String')

You can fix it by using x:Reference:

<TextBlock x:Name="txb2" Text="Hello Stackoverflow"/>

<TextBox Grid.Row="1">
    <TextBox.ToolTip>
        <TextBlock>
            <Run Text="{Binding Source={x:Reference txb2}, Path=Text}" FontWeight="Bold"/>
        </TextBlock>
    </TextBox.ToolTip>
</TextBox>

As for the difference between ElementName and x:Reference take a look at the following thread. ElementName does not work since Tooltip is not a Ui property, but ElementName only works with Ui Element hierarchy (Visual Tree) when it searches txb2.

Access Denied
  • 8,723
  • 4
  • 42
  • 72
  • Thank you. that code is right i found solution thanks to you. (x:Reference) – ijaewung Nov 15 '18 at 23:09
  • Note that can't use x:Reference on an item higher in the VisualTree. There is a circular reference exception with the XamlParser. – CBFT Feb 01 '23 at 18:27
1

Tooltips exist outside the visual tree, so can't reference other controls by name. All that a tooltip knows about is its own PlacementTarget – the UIElement that it is displayed against.

One way to allow the tooltip to reference other controls is to hijack some otherwise unused property of this placement target control (Tag is most often suitable), which can then be referenced by the tooltip.

<TextBox x:Name="txb2" Text="Hello Stackoverflow" Width="200" />

<TextBox Grid.Row="1" Tag="{Binding ElementName=txb2}" Width="200">
    <TextBox.ToolTip>
        <ToolTip DataContext="{Binding PlacementTarget.Tag, RelativeSource={RelativeSource Self}}">
            <TextBlock>
                <Run Text="{Binding Text}" FontWeight="Bold" />
            </TextBlock>
         </ToolTip>
    </TextBox.ToolTip>
</TextBox>

if you're using the MVVM design pattern, an alternative method (that doesn't require property hijacking) is to bind to the PlacementTarget's DataContext (usually the ViewModel). You can then bind the tooltip's content to whatever property of that you like.

<ToolTip DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}">
    ....
Peregrine
  • 4,287
  • 3
  • 17
  • 34
  • just i simply wanted solution. but i got it more important architecture thanks to you. finally i understand :) – ijaewung Nov 15 '18 at 23:13