IgorStack
IgorStack

Reputation: 869

Parameterized style to display hint text in WPF TextBox

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?

Upvotes: 0

Views: 79

Answers (0)

Related Questions