如何清除自动完成框的值?

发布于 2024-12-22 08:30:58 字数 467 浏览 0 评论 0原文

就像 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

送君千里 2024-12-29 08:30:58

您还需要重置所选项目。

private void SearchAutoCompleteBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    SearchAutoCompleteBox.SelectedItem = null;

    SearchAutoCompleteBox.Text = string.Empty;
}

You need reset Selected Item too.

private void SearchAutoCompleteBox_OnPreviewMouseDown(object sender, MouseButtonEventArgs e)
{
    SearchAutoCompleteBox.SelectedItem = null;

    SearchAutoCompleteBox.Text = string.Empty;
}
Hello爱情风 2024-12-29 08:30:58

您是否尝试过在局部变量上设置属性?

acb.Text = string.Empty;

我有一种感觉,当焦点或 textchange 事件触发时,可能会有其他代码影响 .Text 字段。

Have you tried setting the property on your local variable?

acb.Text = string.Empty;

I have a feeling there may be additional code affecting the .Text field when the focus or textchange events are firing.

不即不离 2024-12-29 08:30:58

我想我在花费了大量时间研究这个问题和 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文