0

I'm trying to create parametrized style for displaying hint text in WPF TextBox.
Original code is taken from this SO question (I added binding to HintText property).
It does work w/o binding. But no luck when I bound HintText to Content of Label:

    <Window.Resources>
    <Style x:Key="HintTextStyle" TargetType="{x:Type TextBox}">
        <Style.Resources>
            <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                <VisualBrush.Visual>
                    <Label Content="{Binding Path=(local:HintTextProperties.HintText), RelativeSource={RelativeSource AncestorType={x:Type TextBox}}}"
                           Foreground="LightGray" />
                </VisualBrush.Visual>
            </VisualBrush>
        </Style.Resources>
        <Style.Triggers>
            <Trigger Property="Text" Value="{x:Static sys:String.Empty}">
                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
            </Trigger>
            <Trigger Property="Text" Value="{x:Null}">
                <Setter Property="Background" Value="{StaticResource CueBannerBrush}" />
            </Trigger>
        </Style.Triggers>
    </Style>
</Window.Resources>

<Grid>
    <TextBox Height="23" Margin="100,100" TextWrapping="Wrap" Text="" VerticalAlignment="Top" Width="120"
             local:HintTextProperties.HintText="Some hint text"
             Style="{DynamicResource HintTextStyle}" />

    <Button Content="Button" HorizontalAlignment="Left" Margin="361,166,0,0" VerticalAlignment="Top" Width="75"/>
</Grid>

Dependency property:

public class HintTextProperties
{
    public static string GetHintText(DependencyObject obj)
    {
       return (string)obj.GetValue(HintTextProperty);
    }

    public static void SetHintText(DependencyObject obj, string value)
    {
        obj.SetValue(HintTextProperty, value);
    }

    public static readonly DependencyProperty HintTextProperty =
        DependencyProperty.RegisterAttached(
        "HintText",
        typeof(string),
        typeof(HintTextProperties),
        new FrameworkPropertyMetadata(""));
}    

What is wrong with this code?

mklement0
  • 382,024
  • 64
  • 607
  • 775
IgorStack
  • 799
  • 2
  • 6
  • 22
  • 2
    If you look into Output window you will see binding error. It's because `Label` is not a part of visual tree, so you can't use binding like you did in the style. Since you already have code behind, you could rather create *attached behavior* to change `TextBox.Background` when text is changed. Another option - do not use style, then you can bind by `ElementName`. Third option is to use [BindingProxy](https://thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/) and have hint text property in view model, but then it will be same text for all textboxes. – Sinatr Dec 12 '19 at 08:02
  • you can see this: https://stackoverflow.com/a/21439310/9809143 – Mahdi Asgari Dec 12 '19 at 08:08

0 Answers0