Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4625834
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T03:19:48+00:00 2026-05-22T03:19:48+00:00

I have a template like this, <Style x:Key=WaterMarkTextBoxStyle BasedOn={StaticResource {x:Type TextBox}} TargetType={x:Type TextBox}> <Setter

  • 0

I have a template like this,

<Style x:Key="WaterMarkTextBoxStyle" BasedOn="{StaticResource {x:Type TextBox}}" TargetType="{x:Type TextBox}">
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="{x:Type TextBox}">
                <Grid>
                    <ScrollViewer x:Name="PART_ContentHost" SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" />
                    <TextBlock x:Name="textBlock" Opacity="0.345" Text="Enter Text Here" TextWrapping="Wrap" Visibility="Hidden" />
                </Grid>

                <ControlTemplate.Triggers>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsFocused" Value="False" />
                            <Condition Property="Text" Value="" />
                        </MultiTrigger.Conditions>
                        <Setter Property="Visibility" TargetName="textBlock" Value="Visible" />
                    </MultiTrigger>
                </ControlTemplate.Triggers>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

This seems to work quite nicely as a watermark textbox in WPF, but how can I change what the watermark text will be?

Above it is hard coded to Text = ‘Enter text here’.

If I use the above like this,

<TextBox Style="{StaticResource WaterMarkTextBoxStyle}"></TextBox>

I cannot actually set what the watermark text is.

Ideas?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T03:19:48+00:00Added an answer on May 22, 2026 at 3:19 am

    Use an attached dependency property:

    public static class Watermark
    {
        public static readonly DependencyProperty TextProperty =
            DependencyProperty.RegisterAttached( "Text",
                                                 typeof(Boolean),
                                                 typeof(Watermark),
                                                 new FrameworkPropertyMetadata() );
    
        public static void SetText( UIElement element, Boolean value )
        {
            element.SetValue( TextProperty, value );
        }
    
        public static Boolean GetText( UIElement element )
        {
            return (Boolean)element.GetValue( TextProperty );
        }
    }
    

    Then for your control you’d do something like:

    <TextBox Style="{StaticResource WaterMarkTextBoxStyle}" Watermark.Text="Search" />
    

    Your style would then need to bind to the DP:

    <TextBlock x:Name="textBlock" Opacity="0.345"
               Text="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=(local:Watermark.Text)}"
               TextWrapping="Wrap" Visibility="Hidden" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say we have a XAML code like this: <Style TargetType={x:Type ListBoxItem}> <Setter Property=Template>
If I have a Control with template like this: <Style x:Key=HomeButtonStyle TargetType={x:Type Control} >
I'm trying to do something like this... <Style x:Key="MyBorderStyle" TargetType="Border"> <Setter Property="Padding" Value="{TemplateBinding Padding}"
I have a Style for a TextBox like this: <Style x:Key=TextBox_Standard TargetType={x:Type TextBoxBase} >
I have created my own ControlTemplate for Button, like this: <Style x:Key=LightButtonStyle TargetType={x:Type ButtonBase}>
I have a template like this: <script id=notesTemplate type=text/html> <li class=Note> <div class=NoteDate data-bind=style:
I have ItemsControl with VirtualizingStackPanel as items panel like this: <ItemsControl Style={StaticResource ItemsControl} Name=itemsControl
I have template that looks like this: 100 template<size_t A0, size_t A1, size_t A2,
Assume I have a function template like this: template<class T> inline void doStuff(T* arr)
Imagine I have a template function like this: template<typename Iterator> void myfunc(Iterator a, typename

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.