Franz Gsell
Franz Gsell

Reputation: 1565

Bind to controls property in corresponding style

I want to define a style for the text box and within the style I want to bind to a property which the corresponding style applies to. Something like this:

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
   <Style.Triggers>
      <Trigger Property="IsEnabled" Value="True">
         <Setter Property="ToolTip">
            <Setter.Value>
               <TextBlock Text="{Binding ????Bind to the textbox TEXT property????}">
               </TextBlock>
            </Setter.Value>
         </Setter>
      </Trigger>
   </Style.Triggers>
</Style>

Is this possible at all?

Here the full Window:

<Window x:Class="StyleBinding.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        xmlns:local="clr-namespace:StyleBinding"
        mc:Ignorable="d"
        Title="MainWindow" Height="450" Width="800">
    <Window.Resources>
        <Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="True">
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <TextBlock Text="{Binding Text, RelativeSource={RelativeSource Self}}"/>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
    </Window.Resources>
    <Grid>
        <TextBox HorizontalAlignment="Left" Height="34" Margin="235,140,0,0" TextWrapping="Wrap" IsEnabled="True"
                 Text="Just a simple text" VerticalAlignment="Top" Width="284">
        </TextBox>
    </Grid>
</Window>

Upvotes: 0

Views: 53

Answers (2)

thatguy
thatguy

Reputation: 22119

You just have to use a RelativeSource binding to access the Text property of the TextBox.

<Setter Property="ToolTip" Value="{Binding Text, RelativeSource={RelativeSource Self}}"/>

In case you build a custom tool tip template in your style, you can do it like this.

<Style TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
   <Setter Property="ToolTip">
      <Setter.Value>
         <ToolTip>
            <TextBlock Text="{Binding PlacementTarget.Text, RelativeSource={RelativeSource AncestorType={x:Type ToolTip}}}"/>
         </ToolTip>
      </Setter.Value>
   </Setter>
</Style>

The PlacementTarget of the ToolTip is the TextBox here.

Upvotes: 1

Keithernet
Keithernet

Reputation: 2509

Here's a working example:

<Style BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
    <Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=Text}" />
</Style>

The key is to use a RelativeSource binding of Self.

There's no need to set a trigger on IsEnabled because a ToolTip will only display on an enabled control by default.

Upvotes: 0

Related Questions