WPF Bind to parent property from within nested element using style

20,996

Solution 1

The problem is not with the RelativeSource but with the way you are using the VisualBrush. Recall that Styles are shared between the elements you apply them to. The reason that your example doesn't work is that, in effect you are trying to share a single textbox (the one you tagged "inner") with multiple parent textboxes.

To see why this is a problem, try a thought experiment: The inner textbox gets created once (roughly speaking, this will happen when the style is created). Which of the textboxes that the style gets applied to should be chosen as the ancestor of the inner text box when you use the RelativeSource binding?

This is why DataTemplates and ControlTemplates exist in WPF. Rather than actually instantiate visuals directly, they define a template that allow multiple copies of visuals to be created as needed.

Solution 2

Reativesource doesn't work as expected. It is better to create watermark textbox using control template. But your version could work:

<Window.Resources>
    <Style TargetType="TextBox" x:Key="stlHintbox">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Mode=Self}}" Value="">
                <Setter Property="TextBox.Background">
                    <Setter.Value>
                        <VisualBrush Stretch="None" Visual="{Binding ElementName=hintText}"/>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
</Window.Resources>
<StackPanel>
    <TextBox Tag="hint text" x:Name="myTextBox" Style="{StaticResource stlHintbox}" />
    <Border Visibility="Hidden">
        <TextBlock x:Name="hintText" Text="{Binding Tag, ElementName=myTextBox}" FontStyle="Italic" Foreground="LightGray" />
    </Border>
</StackPanel>
Share:
20,996
veljkoz
Author by

veljkoz

Programming usually in .NET, with SQL Server as backend, but always open to new solutions. Interested in software architecture in general

Updated on August 14, 2020

Comments

  • veljkoz
    veljkoz over 3 years

    I've been trying to build a text box with a hint that's displaying while it's empty. I'm having trouble setting the hint text from within a style.

    To be precise, this works (that is, it binds correctly):

        <TextBox Tag="hint text">
            <TextBox.Background>
                <VisualBrush Stretch="None">
                    <VisualBrush.Visual>
                        <TextBlock Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}" FontStyle="Italic" Foreground="LightGray" />
                    </VisualBrush.Visual>
                </VisualBrush>
            </TextBox.Background>
        </TextBox>
    

    but, when I move it to the Style, it doesn't:

    <Style TargetType="TextBox" x:Key="stlHintbox">
        <Style.Triggers>
            <DataTrigger Binding="{Binding Text, RelativeSource={RelativeSource Mode=Self}}" Value="">
                <Setter Property="Background">
                    <Setter.Value>
                        <VisualBrush Stretch="None">
                            <VisualBrush.Visual>
                                <TextBlock Tag="inner" Text="{Binding Tag, RelativeSource={RelativeSource AncestorType=TextBox}}" 
                                           FontStyle="Italic" Foreground="LightGray" />
                            </VisualBrush.Visual>
                        </VisualBrush>
                    </Setter.Value>
                </Setter>
            </DataTrigger>
        </Style.Triggers>
    </Style>
    
    <TextBox Tag="hint text" Style="{StaticResource stlHintbox}" />
    

    So what's the catch? How can I bind to ancestor property from within a style?