有没有办法将UWP文本框设置为覆盖?

时间:2019-06-29 20:10:39

标签: c# uwp

我需要在UWP中实现一个带覆盖的文本框-当用户在现有文本上键入内容时,我希望删除而不插入旧字符。

我尝试了与WPF相同的解决方案,但是在UWP中没有TextEditor或类似的东西

2 个答案:

答案 0 :(得分:0)

使用PlaceholderText属性作为文本。

答案 1 :(得分:0)

使用GettingFocus事件和PlaceholerText属性。

private void TextBox_GettingFocus(UIElement sender, GettingFocusEventArgs args)
{
    var textbox = (sender as TextBox);
    textbox.PlaceholderText = textbox.Text;
    textbox.Text = "";
}

使用LosingFocus事件检测文本是否已更改,如果文本未更改,请还原旧文本。

private void TextBox_LosingFocus(UIElement sender, LosingFocusEventArgs args)
{
    var textbox = (sender as TextBox);
    // if the user didnt change the text, restore the old text input
    if (textbox.Text == "")
    {
        textbox.Text = textbox.PlaceholderText;
        textbox.PlaceholderText = "";
    }
}
相关问题