0

So, I'm trying to design as minimalistic a UI as possible, and to that end, I need to provide hints inside textboxes, like android does. I've found many solutions to the problem (see Watermark / hint text / placeholder TextBox in WPF , How can I add a hint text to WPF textbox?) but every solution seems to use lots of XAML code, styles, triggers and the sort. What I want to do is, I want to have a textbox subclass that has a HintText property which I can use everywhere, but so far, I haven't managed to even get close. This is the closest I got:

<TextBox x:Class="MyProgram.CustomControls.HintTextBox"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             mc:Ignorable="d" Text="ASDF"
             d:DesignHeight="174" d:DesignWidth="708">
    <TextBox.Resources>
        <VisualBrush x:Key="VB">
            <VisualBrush.Visual>
                <Label Content="{Binding Path=HintText}" Foreground="LightGray" FontSize="25"/>
            </VisualBrush.Visual>
        </VisualBrush>
    </TextBox.Resources>
    <TextBox.Style>
        <Style TargetType="TextBox">
            <Setter Property="Background" Value="{StaticResource VB}"/>
        </Style>
    </TextBox.Style>
</TextBox>

and:

public partial class HintTextBox : TextBox
{
    public HintTextBox()
    {
        InitializeComponent();
    }

    public static DependencyProperty HintTextProperty = DependencyProperty.Register("HintText", typeof(String), typeof(HintTextBox));
}

Which is missing the trigger definitions, but that's not the main problem here. My primary problem is that I can't seem to bind the HintText property. I can't assign it through XAML, and I can't bind to it for some reason. I also tried binding to the TextBox's own Text property just to see if it would work, and it didn't. What am I doing wrong? Or am I barking up the wrong tree entirely?

EDIT: I also need the same functionality for a PasswordBox, getting nowhere with that either... Why the hell did they separate TextBox and PasswordBox anyway?

Community
  • 1
  • 1
Arshia001
  • 1,854
  • 14
  • 19
  • 3
    I recommend you to use the [extended WPF Toolkit](https://wpftoolkit.codeplex.com/). It is open source and comes along with a `WatermarkTextBox`. – Stefan Over Jan 02 '14 at 10:48
  • Great, thanks! Still missing the passwordbox, though... – Arshia001 Jan 02 '14 at 11:00
  • RE: Not being able to bind to your HintText property ... you're missing the property definition (an addition to the dependency property definition). public string HintText { get { return (string)GetValue(HintTextProperty); } set { SetValue(HintTextProperty, value); } } – Mashton Jan 02 '14 at 14:08
  • You know, I thought the clr property was just meant as a shorthand, didn't know it was mandatory. That solves it (part of it anyway), thanks! – Arshia001 Jan 03 '14 at 17:15

1 Answers1

0

The problem is, that the VisualBrush resource "VB" can be shared between all elements and label content can't be binded.

You can try use my sample TextBox with null text hint

Mino
  • 305
  • 2
  • 12
  • Sorry to be a noob, but would you care to elaborate? – Arshia001 Jan 03 '14 at 17:16
  • Ok, I'll try :-) You defined "VB" VisualBrush as resource. In WPF are every resource (styles, colors, ...) shared. So, when we use this resource first time, the Label inside VisualBrush is created. This label is created only one time and used for all textboxes. This is reason, why binding doesn't work. – Mino Jan 03 '14 at 19:31
  • Thanks, I get it now! But how would I fix it? – Arshia001 Jan 04 '14 at 22:35
  • Hi, I apologize for wrong answer. I thought that you had HintTextbox as CustomControl and not as UserControl. So If you want bind Content of Label from VisualBruh you must used RelativeSource binding. {Binding RelativeSource={RelativeSource Mode=FindAncestor, AncestorType={x:Type my:HintTextBox}}, Path=HintText}" my is namespace of HintTextBox. But I thing still, that the correct solution is use custom control and template, which I present in sample http://code.msdn.microsoft.com/TextBox-with-null-text-hint-0b384543 – Mino Jan 06 '14 at 15:24