WPF Bind to parent property from within nested element using style WPF Bind to parent property from within nested element using style wpf wpf

WPF Bind to parent property from within nested element using style


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.


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>