如何清除自动完成框的值?
就像 WPF 中的许多事情一样,有时最简单的事情也是最难找到示例的事情!如何清除 AutoCompleteBox 的当前文本?在我的 OnFocus 事件中,我想确保为用户提供一个清晰的输入框。所以我的事件过程看起来像
private void autGlobal_GotFocus(object sender, RoutedEventArgs e)
{
AutoCompleteBox acb = (AutoCompleteBox)sender;
if (acb.SearchText == "Search Term")
{
//clear out the box if it has the focus
this.autGlobal.Text = "";
}
}
但是,直接设置文本属性似乎不起作用。我错过了一些明显的东西吗?
Just like many things in WPF, sometimes the easiest things are the ones that are hardest to find examples for! How do you clear out the current text of an AutoCompleteBox? In my OnFocus event I want to make sure that the user is given a clear box for entry. So my event procedure looks like
private void autGlobal_GotFocus(object sender, RoutedEventArgs e)
{
AutoCompleteBox acb = (AutoCompleteBox)sender;
if (acb.SearchText == "Search Term")
{
//clear out the box if it has the focus
this.autGlobal.Text = "";
}
}
However, setting the text property directly does not seem to work. Am I missing something obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您还需要重置所选项目。
You need reset Selected Item too.
您是否尝试过在局部变量上设置属性?
我有一种感觉,当焦点或 textchange 事件触发时,可能会有其他代码影响 .Text 字段。
Have you tried setting the property on your local variable?
I have a feeling there may be additional code affecting the .Text field when the focus or textchange events are firing.
我想我在花费了大量时间研究这个问题和 XAML 后找到了答案。当 XAML 中的 IsTextCompletionEnabled 选项设置为 true 时,此代码示例将不起作用。我将其设置为 false 并且此代码运行良好。
I think I found the answer after spending a ton of time on this and the XAML. This code example will not work when the IsTextCompletionEnabled option is set to true in the XAML. I set it to false and this code works fine.