在Xamarin
上有一个名为Entry
的控件。它支持TextPreview
这是一个默认的文本,当TextBox为空时显示在“背景”中。WPF Textbox上的TextPreview
我使用How can I add a hint text to WPF textbox?使它在单个TextBox
上工作。现在我想让这个可重用(创建一个CustomControl
在WPF
)。我也试图把它变成一个全球性的Style
here,但我没有真正得到我想要的。 -
长话短说:我该如何获得此CustomControl的工作?
我不能比这得到任何进一步的:
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); }
}
}
我的这个想法:
是否可以注册第二个事件,以现有的财产? 如果是这样,我想附加我的第二个EventHandler到TextChanged
。 一旦Text
被清除,我想Preview
显示出来。
为了把事情说清楚:
我想创建一个CustomControl - 没有解决方法。 由于它在Xamarin.Forms.Entry
中实现,因此它是可能的。
这就是所谓的水印。看到这个问题。 ^^ –
@LynnCrumbling很好...水印的故事是一个解决方案,让它工作。正如我上面所描述的,我已经使用'Syles'工作。您提供的“重复”不是关于创建自定义控件。 –
我在该问题上看到多个答案,使用文本框派生的控件提供解决方案。我认为你没有检查所有的答案。 –