0

This is my working XAML code for a tooltip:

 <DataTrigger Binding="{Binding Visibility,ElementName=txtParent}" Value="Visible">
       <Setter Property="StackPanel.ToolTip" Value="{Binding Text,ElementName=txtParent}" TargetName="PanelTitle">
       </Setter>   
 </DataTrigger>

Now I want a tool tip which has maximum width as well as text wrapping. I tried using the following code but it didnt work:

 <DataTrigger Binding="{Binding Visibility,ElementName=txtParent}" Value="Visible"> 
     <Setter Property="StackPanel.ToolTip" TargetName="PanelTitle">
           <Setter.Value>
                <ControlTemplate TargetType="{x:Type ToolTip}">
                <StackPanel Margin="7,1">                                               
                   <TextBlock Text="{Binding Text,ElementName=txtParent}"  Margin="1" HorizontalAlignment="Center"                          Width="200"                         TextWrapping='Wrap' VerticalAlignment="Top" />                                               
                </StackPanel>
            </ControlTemplate>
        </Setter.Value>
     </Setter>
 </DataTrigger>
alex
  • 5,516
  • 2
  • 36
  • 60
zooney
  • 341
  • 6
  • 25

1 Answers1

3

A ToolTip does not exist in the visual tree in the way you might expect. It does not participate in the inheritance context or the "owner" element's name scope at runtime. I would guess that your binding is unable to resolve txtParent at runtime because it doesn't know where to look.

You could try replacing ElementName=txtParent in your binding with Source={x:Reference txtParent}, which forces the name to resolve during the parsing stage, when the name will still be in scope. It may work, and it may not, but either way it will likely break the XAML designer.

Also, you are setting the value of the ToolTip to a ControlTemplate, which doesn't make much sense. You should probably be setting it to a ToolTip instance (or some other content type):

<Setter.Value>
  <ToolTip>
    <StackPanel Margin="7,1" >                                               
      <TextBlock Text="{Binding Text,Source={x:Reference txtParent}}"
                 Margin="1"
                 HorizontalAlignment="Center"
                 Width="200"
                 TextWrapping="Wrap"
                 VerticalAlignment="Top" />
    </StackPanel>
  </ToolTip>
</Setter.Value>

Another hack which might work is to store the ToolTip as a resource somewhere in scope and have your Setter assign it with a StaticResource reference. Generally, using UI elements as shared resources is error prone and considered bad practice, but seeing as only one ToolTip should ever be loaded at any given time in your application, it may work:

<FrameworkElement.Resources>
  <ToolTip x:Key="MyToolTip">
    <StackPanel Margin="7,1" >                                               
      <TextBlock Text="{Binding Text,Source={x:Reference txtParent}}"
                 Margin="1"
                 HorizontalAlignment="Center"
                 Width="200"
                 TextWrapping="Wrap"
                 VerticalAlignment="Top" />
    </StackPanel>
  </ToolTip>
</FrameworkElement.Resources>

<!-- ... -->

<DataTrigger Binding="{Binding Visibility, ElementName=txtParent}"
             Value="Visible">
  <Setter Property="StackPanel.ToolTip"
          Value="{StaticResource MyToolTip}"
          TargetName="PanelTitle" />
</DataTrigger>

If you are using MVVM, though, the easiest (and probably best) thing to do would be to bind txtParent.Text to a property on your view model; then you could use the technique from this other answer to bind to the same property for your ToolTip.

Community
  • 1
  • 1
Mike Strobel
  • 25,075
  • 57
  • 69
  • Hey Thanks!!! As rightly pointed out by you , the first piece of code breaks the XAML designer. And the second one says Object reference not set to an instance of an object.Any help? – zooney Feb 04 '14 at 14:49
  • The `ToolTip` resource may need to be declared *after* the `txtParent` element in XAML. Can you shift your resource declarations around and see if that helps? I tested my examples in Kaxaml, and they both worked (except that I didn't have a `TargetName` in my `Setter`). – Mike Strobel Feb 04 '14 at 14:51
  • I changed Source={x:Reference txtParent} to Element Name, It gave no error . But now it says the resource MyToolTip cannot be resolved.Even though I changed the declaration of ToolTip after the txtParent. – zooney Feb 04 '14 at 14:55
  • Are you using MVVM? This would be easier if your `TextBox.Text` was data-bound to a property on your view model; then you could use [this technique](http://stackoverflow.com/a/14755540/302677) to bind to the same property for your `ToolTip`. – Mike Strobel Feb 04 '14 at 15:00
  • No Im not using MVVM, Basic Over view is : .. – zooney Feb 04 '14 at 15:01
  • The `ToolTip` resource has to be declared before the `Style` with the `DataTrigger` referencing it. And I suspect `` has to be declared *after* `txtParent` too. – Mike Strobel Feb 04 '14 at 15:09
  • Problem is my txtParent is defined in the style itself!!!. Any way to do it?Output shows no data in the tool tip. – zooney Feb 04 '14 at 15:15
  • Put the resources under `Style.Resources` at the end of the `Style` declaration? – Mike Strobel Feb 04 '14 at 15:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/46765/discussion-between-zooney-and-mike-strobel) – zooney Feb 04 '14 at 15:24
  • Unfortunately, StackOverflow chat is blocked at my office :(. – Mike Strobel Feb 04 '14 at 15:30