如何在 Windows Phone 7 中关闭自动完成框的下拉列表

发布于 2024-11-26 22:18:26 字数 1364 浏览 0 评论 0原文

无论如何,有没有以编程方式关闭自动完成框的下拉列表的方法?我的用例如下。

MainPage.xaml 将值传递给SearchPage.xaml(即/SearchPage.xaml?query=someText)。

在 SearchPage.xaml.cs 中,我设置了

autoCompleteBox.Text = NavigationContext.QueryString["query"]. 

此时显示建议匹配的下拉列表。当页面刚刚导航到时我不希望出现这种行为。

我还尝试了以下方法来关闭下拉列表,但没有帮助。

autoCompleteBox.Text = NavigationContext.QueryString["query"];
autoCompleteBox.IsDropDownOpen = false;

当失去焦点时,下拉列表似乎会远离 AutoCompleteBox,但我没有看到要设置的属性/字段以使其失去焦点。

任何帮助表示赞赏。

好吧,我稍微修改了一下,想出了一个拼凑的办法。在 SearchPage.xaml.cs 的构造函数中,我有以下代码。

autoCompleteBox.TextFilter += DummyFilter;
autoCompleteBox.GotFocus += (s,args) => {
 if(!isAutoCompleteBoxInit) {
  autoCompleteBox.TextFilter -= DummyFilter;
  autoCompleteBox.TextFilter += RealFilter;
 }
}

DummyFilter 如下所示。

bool DummyFilter(string search, string value) { return false; }

RealFilter 如下所示。

bool RealFilter(string search, string value) {
 if(null != value) return value.ToLower().StartsWith(search.ToLower());
}

在我的 OnNavigedTo 方法中,我设置了 autoCompleteBox.Text = NavigationContext.QueryString["query"]。所以当我现在这样做时,DummyFilter 将始终返回 false,因此下拉列表消失。当用户关注 AutoCompleteBox 时,我检查正确的 Filter 是否已附加到 TextFilter 属性,如果没有,则进行切换。

希望这对你们中的一些人有帮助。

is there anyway to programmatically dismiss the drop-down list of an autocompletebox? my use case is as follows.

MainPage.xaml passes a value to SearchPage.xaml (i.e. /SearchPage.xaml?query=someText).

in SearchPage.xaml.cs, i set,

autoCompleteBox.Text = NavigationContext.QueryString["query"]. 

at this point, the drop-down list of suggested matches shows up. i don't want this behavior when the page is just navigated to.

i also tried the following to dismiss the drop-down list but it didn't help.

autoCompleteBox.Text = NavigationContext.QueryString["query"];
autoCompleteBox.IsDropDownOpen = false;

the drop-down list seems to go away from the AutoCompleteBox when it loses focuses, but i don't see a property/field to set to make it lose focus.

any help is appreciated.

well, i tinkered a bit and came up with a kludge. in the constructor of SearchPage.xaml.cs i have the following code.

autoCompleteBox.TextFilter += DummyFilter;
autoCompleteBox.GotFocus += (s,args) => {
 if(!isAutoCompleteBoxInit) {
  autoCompleteBox.TextFilter -= DummyFilter;
  autoCompleteBox.TextFilter += RealFilter;
 }
}

DummyFilter looks like the following.

bool DummyFilter(string search, string value) { return false; }

RealFilter looks like the following.

bool RealFilter(string search, string value) {
 if(null != value) return value.ToLower().StartsWith(search.ToLower());
}

in my OnNavigatedTo method, is where i set, autoCompleteBox.Text = NavigationContext.QueryString["query"]. so when i do this now, the DummyFilter will always return false, so the drop-down list goes away. when the user focuses in on the AutoCompleteBox, i check if the correct Filter was already attached to the TextFilter property, if not, then i do a switch.

hope this helps some of you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

梦行七里 2024-12-03 22:18:26

页面上还有其他可聚焦的控件吗?只要把焦点放在其他地方,你的问题就应该得到解决。

Is there any other focusable control on the page? Just set the focus somewhere else, and your problem should be solved.

茶底世界 2024-12-03 22:18:26

当您更改自动完成框的文本后,下拉菜单将打开。仅当用户更改文本并且存在匹配项时,下拉菜单才会关闭。

只需将 userInitiated 更改为 true,当有匹配项时,下拉列表将关闭。

private void UpdateTextCompletion(bool userInitiated)
    {
        userInitiated = true; ...

When you have changed the text of the AutoCompleteBox the dropdown will open. Only when the user has changed the text and there is a match then the dropdown will close.

Just change userInitiated to true and when there is a match the dropdown will close.

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