2
<Style TargetType="TextBox">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate>
                <Grid>
                    <Border BorderBrush="Red" CornerRadius="2" />
                    <ScrollViewer x:Name="PART_ContentHost"  VerticalAlignment="Center"         HorizontalAlignment="Center"/>
                    <Label x:Name="watermarklabel" Height="40" Content="{TemplateBinding Tag}" Foreground="Gray"/>
                </Grid>

                <ControlTemplate.Triggers>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Value="True">
                                <Condition.Binding>
                                    <MultiBinding Converter="{StaticResource Multi}">
                                        <!--<Binding Path="Text" ElementName="txt1"/>-->
                                        <Binding RelativeSource="{RelativeSource Mode=TemplatedParent}"  Path="Text"/>
                                    </MultiBinding>
                                </Condition.Binding>
                            </Condition>
                        </MultiDataTrigger.Conditions>

                        <Setter Property="Visibility" TargetName="watermarklabel" Value="Collapsed"/>
                    </MultiDataTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

Here i want to enable/disable the label (which is water mark) upon the condition when the textbox text is empty and not empty. In the multi binding i am trying to access the text property using the TemplatedParent. But it is not hitting convertor when text changed.

When i use the element name to access it everything is fine.

But i want to make generic this one..

How to make this to work?

Thanks in advance..

javi
  • 159
  • 2
  • 15
NANDAKUMAR THANGAVELU
  • 611
  • 3
  • 15
  • 34
  • How is your question different from the hundreds of other similarly-titled questions already asked here: [Watermark / hint text / placeholder TextBox in WPF](http://stackoverflow.com/q/833943), [How can I add a hint text to WPF textbox?](http://stackoverflow.com/q/7425618), [Place holder or watermark in TextBox windows 8](http://stackoverflow.com/q/12172170), [how to set some default text at the passwordBox wpf?](http://stackoverflow.com/q/13043906), [Create WPF Watermark in Pure XAML](http://stackoverflow.com/q/10687596), etc. – Cody Gray - on strike May 22 '14 at 10:11

1 Answers1

1

This is actually surprisingly tricky to get right. There are a dozen different almost-right answers on the internet, but few or no completely correct ones.

The closest I've ever come to getting this to work correctly is to dynamically inject a textblock into the standard textbox's ControlTemplate at runtime based on the conditions that you're evaluating.

  1. Dynamic injection avoids having to re-implement the entire controltemplate and, assuming the control template doesn't transform too much between versions, also avoids the maintenance point of having to create new ControlTemplates every time built-in theming changes.

  2. Injecting an element instead of modifying the existing text element avoids all kinds of problems where users can select/manipulate the watermark text in undesirable ways.

  3. Avoid an overlaying solution: the z-order and clipping issues aren't worth the effort (I once tried to do this with a decorator, not the right call)

  4. Don't forget to verify that Drag/Drop and Copy/Paste operations on the text box work as expected with the watermark.

  5. IIRC, I did it with an attached property so that a watermark didn't require a new control. Rather, it searched the visual tree for the first viable watermark target it could find and applied the watermark there. This let it work in a combobox as well, e.g., without add'l work.

Greg D
  • 43,259
  • 14
  • 84
  • 117
  • Hi. Thanks for ur reply. I saw this by using attached property (ur 5th point).. And i am new to this WPF. Could u pls provide a sample example on 1-4 points.?? – NANDAKUMAR THANGAVELU May 22 '14 at 05:04
  • I'm afraid I no longer have the code, so I don't have a sample handy. Point #4 is just usual UI testing, nothing fancy there. For the runtime template modification I suggest using the VisualTreeHelper (http://msdn.microsoft.com/en-us/library/system.windows.media.visualtreehelper%28v=vs.110%29.aspx) and Snoop to actually add the element to the template and figure out where in the template it should go. – Greg D May 22 '14 at 07:02
  • Ok. But i am having still one query. In multibinding i am trying to access its parent element by using "TemplatedParent". But by doing so, convertor not hitting. Whether i am wrong or right. Why it is not hitting.When i use element name it is fine. What i have to do to correct that problem?? – NANDAKUMAR THANGAVELU May 22 '14 at 09:29