如何在 Windows Phone 7 中关闭自动完成框的下拉列表
无论如何,有没有以编程方式关闭自动完成框的下拉列表的方法?我的用例如下。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
页面上还有其他可聚焦的控件吗?只要把焦点放在其他地方,你的问题就应该得到解决。
Is there any other focusable control on the page? Just set the focus somewhere else, and your problem should be solved.
当您更改自动完成框的文本后,下拉菜单将打开。仅当用户更改文本并且存在匹配项时,下拉菜单才会关闭。
只需将 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.