0

I would like to add a watermark to a textbox, while using the MVVM design pattern.

After browsing several hours I have found a solution to my problem - but my solution doesn't work!

The following code is placed in a UserControl, where the textbox should have a watermark saying "Insert Fields.." - or something like it. The watermark disappers when the user types something in the textbox. But I only see an empty textbox; no watermark is visible.

<TextBox x:Name="txtFields" Height="23" TextWrapping="Wrap" Background="#FFCBEECD" AcceptsReturn="True" >
    <TextBox.InputBindings>
        <KeyBinding Key="Enter" Command="{Binding AddFieldCommand}"></KeyBinding>
    </TextBox.InputBindings>
</TextBox>

<!--Watermark for TextBox-->
<TextBlock IsHitTestVisible="False" Text="Insert Fields.." VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0" Foreground="DarkGray">
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Text, ElementName=txtFields}" Value="">
                    <Setter Property="Visibility" Value="Visible" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

Does see why this code doesn't work?

Thanks,

SOLUTION:

<Grid>
    <TextBox x:Name="txtMethods" Height="23" TextWrapping="Wrap" Text="Insert Methods" Background="#FFCBEECD" AcceptsReturn="True">
        <TextBox.InputBindings>
            <KeyBinding Key="Enter" Command="{Binding AddMethodCommand}"></KeyBinding>
        </TextBox.InputBindings>
    </TextBox>

    <TextBlock IsHitTestVisible="False" Text="Insert Methods.." VerticalAlignment="Center" HorizontalAlignment="Left" Margin="10,0,0,0" Foreground="DarkGray">
        <TextBlock.Style>
            <Style TargetType="{x:Type TextBlock}">
                <Setter Property="Visibility" Value="Collapsed"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding Text, ElementName=txtMethods}" Value="">
                        <Setter Property="Visibility" Value="Visible"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>
</Grid>

This seems to do the trick for me:

daniele3004
  • 13,072
  • 12
  • 67
  • 75
MikaelKP
  • 133
  • 2
  • 18
  • Could you describe a solution you have seen and the problem you have with it? A watermark is view-only; so doesn't need a viewmodel or anything like that. – BradleyDotNET Nov 18 '14 at 19:12
  • @BradleyDotNET I have read solution, where a textblock have been used as a layer above the textbox. The textblock is removed then the user access the textbox. One of them is here: [link](http://stackoverflow.com/questions/833943/watermark-hint-text-placeholder-textbox-in-wpf) – MikaelKP Nov 18 '14 at 19:17
  • 1
    Is the following text box what you are looking for ...https://wpftoolkit.codeplex.com/wikipage?title=WatermarkTextBox – Stuart Smith Nov 18 '14 at 19:17
  • @MikaelKP Okay; not super elegant; but whats wrong with that? – BradleyDotNET Nov 18 '14 at 19:18
  • @BradleyDotNET It won't work at all.. Only a empty textbox appears. – MikaelKP Nov 18 '14 at 19:22
  • Clearly you did it wrong then :) Perhaps you should post *that* code and ask whats wrong. Or just use the one from WPF Toolkit – BradleyDotNET Nov 18 '14 at 19:23
  • @BradleyDotNET I have updated the post with the code – MikaelKP Nov 18 '14 at 19:33
  • I don't see anything *directly* wrong with that; but it doesn't look like any of the answers in the linked post. Have you tried this approach? http://stackoverflow.com/a/836463/1783619 – BradleyDotNET Nov 18 '14 at 19:40
  • You should self-answer if you have a solution (preferably with an explanation of *why* the code fixes it, along with the code). Solutions don't belong in questions. – BradleyDotNET Nov 18 '14 at 21:19
  • @BradleyDotNET I don't have enough reputation, but as soon as I have, I will self-answer :) – MikaelKP Nov 18 '14 at 21:34
  • I didn't know there was a rep requirement for that. Oh well, glad you got it working! – BradleyDotNET Nov 18 '14 at 21:38

0 Answers0