Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am creating textBox dynamically and applying style for watermark text. its working fine but, The text displayed as watermark label(Only Numeric) that is hard coded in my style that I want to set in code behind,
below are my style. I want to pass it from codebehind.
thanks.

What I have tried:

<Style x:Key="onlyNumericTextboxStyle" TargetType="{x:Type TextBox}">
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Grid>
                            <Border Background="{TemplateBinding Background}" BorderBrush="#FF97A0A5" BorderThickness="1" SnapsToDevicePixels="True">
                                <ScrollViewer x:Name="PART_ContentHost" Margin="0,0,0,0" VerticalAlignment="Center" />
                            </Border>
                            <Label x:Uid="UsernameText" x:Name="WaterMarkLabel" Content="Only Numeric"
                               Visibility="Collapsed" Foreground="#1a1a1a" FontSize="10" Opacity="0.5"
                                   FontStyle="Italic"/>
                        </Grid>
                        <ControlTemplate.Triggers>
                            <MultiTrigger>
                                <MultiTrigger.Conditions>
                                    <Condition Property="Text" Value=""/>
                                </MultiTrigger.Conditions>
                                <Setter Property="Visibility" TargetName="WaterMarkLabel" Value="Visible"/>
                            </MultiTrigger>
                            <Trigger Property="IsEnabled" Value="False">
                                <Setter Property="Foreground" Value="DimGray"/>
                            </Trigger>
                        </ControlTemplate.Triggers>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
        </Style>
Posted
Updated 8-Jan-18 0:11am
v2

Here is a working version of what you are trying to do:
XML
<Window x:Class="MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

        Title="Watermark Textbox demo" Height="350" Width="525">

    <Window.Resources>

        <SolidColorBrush x:Key="TextBox.Static.Border" Color="#FFABAdB3"/>
        <SolidColorBrush x:Key="TextBox.MouseOver.Border" Color="#FF7EB4EA"/>
        <SolidColorBrush x:Key="TextBox.Focus.Border" Color="#FF569DE5"/>

        <Style x:Key="CommandTextBoxStyle" TargetType="{x:Type TextBox}">
            <Setter Property="Background"
                    Value="{DynamicResource {x:Static SystemColors.WindowBrushKey}}"/>
            <Setter Property="BorderBrush"
                    Value="{StaticResource TextBox.Static.Border}"/>
            <Setter Property="Foreground"
                    Value="{DynamicResource {x:Static
                                                 SystemColors.ControlTextBrushKey}}"/>
            <Setter Property="BorderThickness" Value="1"/>
            <Setter Property="KeyboardNavigation.TabNavigation" Value="None"/>
            <Setter Property="HorizontalContentAlignment" Value="Left"/>
            <Setter Property="FocusVisualStyle" Value="{x:Null}"/>
            <Setter Property="AllowDrop" Value="true"/>
            <Setter Property="ScrollViewer.PanningMode" Value="VerticalFirst"/>
            <Setter Property="Stylus.IsFlicksEnabled" Value="False"/>
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="{x:Type TextBox}">
                        <Border x:Name="border"
                                BorderBrush="{TemplateBinding BorderBrush}"
                                BorderThickness="{TemplateBinding BorderThickness}"
                                Background="{TemplateBinding Background}"
                                SnapsToDevicePixels="True">
                            <Grid>
                                <ScrollViewer x:Name="PART_ContentHost"
                                              Focusable="false"
                                              HorizontalScrollBarVisibility="Hidden"
                                              VerticalScrollBarVisibility="Hidden"/>

                                <TextBlock IsHitTestVisible="False"
                                           Margin="{TemplateBinding Padding}"
                                           Text="Watermark text goes here"
                                           TextTrimming="CharacterEllipsis"
                                           FontStyle="Italic" Foreground="LightGray"
                                           DataContext="{Binding RelativeSource=
                                               {RelativeSource Mode=FindAncestor,
                                                               AncestorType=TextBox}}">
                                    <TextBlock.Style>
                                        <Style TargetType="{x:Type TextBlock}">
                                            <Setter Property="Visibility" 
                                                    Value="Collapsed"/>
                                            <Style.Triggers>
                                                <MultiDataTrigger>
                                                    <MultiDataTrigger.Conditions>
                                                        <Condition Binding="{Binding 
                                                                   Text}" Value=""/>
                                                        <Condition Binding="{Binding 
                                                                   IsKeyboardFocused}"
                                                                   Value="False"/>
                                                    </MultiDataTrigger.Conditions>
                                                    <MultiDataTrigger.Setters>
                                                        <Setter Property="Visibility" 
                                                                Value="Visible"/>
                                                    </MultiDataTrigger.Setters>
                                                </MultiDataTrigger>
                                            </Style.Triggers>
                                        </Style>
                                    </TextBlock.Style>
                                </TextBlock>

                            </Grid>
                        </Border>
                    </ControlTemplate>
                </Setter.Value>
            </Setter>
            <Style.Triggers>
                <MultiTrigger>
                    <MultiTrigger.Conditions>
                        <Condition Property="IsInactiveSelectionHighlightEnabled" 
                                   Value="true"/>
                        <Condition Property="IsSelectionActive" Value="false"/>
                    </MultiTrigger.Conditions>
                    <Setter Property="SelectionBrush"
                            Value="{DynamicResource {x:Static 
                                  SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                </MultiTrigger>
            </Style.Triggers>
        </Style>

    </Window.Resources>

    <Grid HorizontalAlignment="Center">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="Auto"/>
            <ColumnDefinition Width="Auto"/>
        </Grid.ColumnDefinitions>
        <TextBox Width="200" VerticalAlignment="Center" Padding="4"
                 Style="{DynamicResource CommandTextBoxStyle}"/>
        <Button Content="Search"
                Margin="10 0" Padding="10 5"
                Grid.Column="1" VerticalAlignment="Center"/>
    </Grid>

</Window>
 
Share this answer
 
v2
Comments
Member 11859517 9-Jan-18 4:08am    
Thanks Graeme_Grant for your response,
but I want to set watermark text from the code behind. how to do this?
Graeme_Grant 9-Jan-18 4:20am    
The solution would be to create a subclassed custom control of the TextBox control and expose a WatermarkText dependency property. Have a read of this article to see how to do it: Simplest WPF Dependency Property For Beginners On 'ValidState'[^]

However, it is still possible to do it this way also. You would need to wait for the control to apply the template, then traverse the visual tree until you find the watermark textblock, then set the text property.
If its in ControlTemplate it would be a messy thing to do... I would not recomend that. You would have to find that specific control in the applied template and that would cost you a lot.
Use bindings or behaviours instead...

And second... Don't use Labels, use TextBlocks.
 
Share this answer
 
Comments
Graeme_Grant 8-Jan-18 6:17am    
Agreed wholly. He would require a working example for this solution to help any.

However, personally, I would inherit the textbox and create a Watermark lookless control class. This however is outside the scope of this Q&A.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900