在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'工作。您提供的「重複」不是關於創建自定義控件。 –
我在該問題上看到多個答案,使用文本框派生的控件提供解決方案。我認爲你沒有檢查所有的答案。 –