On Xamarin there is a Control called Entry. It supports a TextPreview which is like a default Text to show in the "background" when a TextBox is empty.
I used How can I add a hint text to WPF textbox? to get it working on a single TextBox. Now I want to make this reusable (Create a CustomControl in WPF). I also tried to forge it into a global Style here but I did't really got what i wanted. -
Long story short: How can I get this CustomControl working ?
I cannot get any further than this:
public class TextboxWithPreview : TextBox
{
public TextboxWithPreview()
{
if(DesignerProperties.GetIsInDesignMode(this))
{
this.TextPreview = "Default TextPreview";
}
EventManager.RegisterClassHandler(typeof(TextboxWithPreview), TextChangedEvent, new TextChangedEventHandler(OnTextChanged));
}
public static readonly DependencyProperty TextPreviewProperty = DependencyProperty.Register("TextPreview", typeof(string), typeof(TextboxWithPreview));
private static void OnTextChanged(object sender, TextChangedEventArgs e)
{
//pseudo Code:
if(string.IsNullOrEmpty(this.Text))
{
this.Text = TextPreview;
this.ForeColor = Colors.Gray;
}
}
public string TextPreview
{
get { return (string)GetValue(TextPreviewProperty); }
set { SetValue(TextPreviewProperty, value); }
}
}
My thoughts about this:
Is it possible to register a second event to an existing property ?
If so I'd like to attach my 2nd EventHandler to TextChanged.
As soon as the Textgets cleared I want the Preview to show up.
To make things clear:
I want to create a CustomControl - no workarounds.
Since it is implemented in Xamarin.Forms.Entry it is definedly possible.