Faure Ugo
Faure Ugo

Reputation: 69

Bind an Inner Label Text to the component ToolTip

I'm actually overriding the ControlTemplate of the default WPF TextBox and i'd like to add a Title to it. The idea here is that the title will be displayed as a placeholder when the TextBox is empty and not focused, and will slide up at the top to become the title in the other cases. As the TextBox don't have a PlaceHolder or a Title property, i thought i could bind my title TextBlock to the ToolTip in order to be able to do this: <TextBox ToolTip="MyTextBoxTitle"/>

My template looks like this :

<ControlTemplate TargetType="{x:Type TextBox}">
  <Border x:Name="border" BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
    <Grid>
      <Grid.RowDefinitions>
        <RowDefinition Height="14"/>
        <RowDefinition/>
        <RowDefinition/>
      </Grid.RowDefinitions>
      <TextBlock x:Name="Title" Text="{Binding Path=ToolTip}" Margin="8,14,0,0" Grid.Row="0" Grid.RowSpan="2" Foreground="{StaticResource TextBox.Static.Border}"></TextBlock>
      <ScrollViewer x:Name="PART_ContentHost" Focusable="false" HorizontalScrollBarVisibility="Hidden" VerticalScrollBarVisibility="Hidden" Grid.Row="1"/>
      <Rectangle x:Name="UnfocusedUnderLine" Fill="{StaticResource TextBox.Static.Border}" Height="6" Margin="0,1,0,0" Grid.Row="2"/>
      <Rectangle x:Name="UnderLine" Fill="{StaticResource TextBox.Focus.Border}" Height="6" Margin="0,1,0,0" Grid.Row="2" Width="0"/>
    </Grid>
  </Border>
  <ControlTemplate.Triggers>
    ...
  </ControlTemplate.Triggers>
</ControlTemplate>

But my Binding on the ToolTip doesn't work, and i can't manage to find a way to make it work, do you know how to do this ?

Upvotes: 1

Views: 78

Answers (1)

dymanoid
dymanoid

Reputation: 15197

Use:

Text="{TemplateBinding ToolTip}"

You need a binding whose source is not the DataContext but the templated element which is TextBox in your case.

Upvotes: 1

Related Questions