0

In my Window.Resources I have the following style:

    <Style TargetType="TextBox" x:Key="HintText" xmlns:sys="clr-namespace:System;assembly=mscorlib">
        <Style.Resources>
            <VisualBrush x:Key="CueBannerBrush" AlignmentX="Left" AlignmentY="Center" Stretch="None">
                <VisualBrush.Visual>
                    <Label Content="{DynamicResource EmptyText}" 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>
            <Trigger Property="IsKeyboardFocused" Value="True">
                <Setter Property="Background" Value="White" />
            </Trigger>
        </Style.Triggers>
    </Style>

If I use this for 1 TextBox with this,

<Label Content="Test" Foreground="LightGray" />

Test will show up in my TextBox if it's empty. When I try to use this style in different TextBoxes with this,

<Label Content="{DynamicResource EmptyText}" Foreground="LightGray" />

and

<TextBox.Resources>
    <sys:String x:Key="EmptyText">Test</sys:String>
</TextBox.Resources>

it doesn't show anything. Is it possible to use this 1 style with a different string that is shown in the TextBox or do I have to make a different style for each TextBox?

Krowi
  • 1,565
  • 3
  • 20
  • 40
  • You seem to be after a *watermark `TextBox`*... there are many ways of creating these and you can find what they are in the [Watermark / hint text TextBox in WPF](http://stackoverflow.com/questions/833943/watermark-hint-text-textbox-in-wpf) question... I believe that you'll find that some at least, are better solutions than your proposed solution. – Sheridan Feb 27 '14 at 21:24

1 Answers1

0

You don't appear to be employing this style in any of the examples you give and it isn't at all clear what relationship your last XAML block has with the one before it.

However, yes you should be able to redefine EmptyText as often as you like. The Text property will be resolved in accordance with the Dependency Property value precedence rules.

So you can do something like this:

<DockPanel HorizontalAlignment="Stretch">
    <DockPanel.Resources>
        <Style TargetType="TextBlock">
            <Setter Property="Text"
                    Value="{DynamicResource EmptyText/>
        </Style>
        <sys:String x:Key="EmptyText">Defined in the Dockpanel resource</sys:String>
    </DockPanel.Resources>

    <TextBlock/>

    <TextBlock>
        <TextBlock.Resources>
            <sys:String x:Key="EmptyText">Defined in the textbox resource</sys:String>
        </TextBlock.Resources>
    </TextBlock>

    <TextBlock>
        <TextBlock.Resources>
            <sys:String x:Key="EmptyText">Also defined at the textbox</sys:String>
        </TextBlock.Resources>
    </TextBlock>
</DockPanel>
mcwyrm
  • 1,571
  • 1
  • 15
  • 27
  • What about if the style is in the Window.resources? Will this still work? – Krowi Feb 27 '14 at 18:03
  • There's something wrong w/ your visual brush. You can use Snoop to see that the brush is getting applied. Don't bother w/ the style; apply the brush directly to a textbox and get it to work the way you want it to first. – mcwyrm Feb 27 '14 at 21:17
  • You might also take a look at [this](http://davidowens.wordpress.com/2009/02/18/wpf-search-text-box/) or [this](http://stackoverflow.com/questions/833943/watermark-hint-text-textbox-in-wpf) as well. – mcwyrm Feb 27 '14 at 21:19
  • I got the style to work as long as the Content of the Label doesn't has a binding. Writing Content="Test" will do what I want for the textboxes with the hint text Test. If I want another text than Test I either copy the style and change the Content and name of the style, but that looks like it is inefficient to me. (This is based on my original code in the 1st message) – Krowi Feb 27 '14 at 21:26
  • I can duplicate the behavior you're describing; I don't have a good explanation for it. There are much better ways to get the behavior you seem to be trying to achieve. You can read about some of them [here](http://stackoverflow.com/questions/18431043/wpf-relativesource-in-style). If you really must persist in this approach, you'll find that it works as you expect if you make the visual brush your dynamic resource (instead of the hint string) and define it one each of your controls. – mcwyrm Feb 28 '14 at 12:59