自动完成框和搜索文本清除
我在 MVVM Silverlight 应用程序中清除 SearchText 时遇到问题。我可以清除 SelectedItem 和 Text,但 SearchText 被留下。它是只读的,不能通过绑定进行更改。
示例:带有国家/地区列表的 AutoCompleteBox。当用户想要输入澳大利亚时,他们输入“au”,此时列表将显示奥地利和澳大利亚。然后用户可以选择澳大利亚并继续。编辑结束后,他们单击“保存”按钮。此时,您可能希望清除数据表单以输入新数据。
即使您绑定了 SelectedItem 和 Text 属性,并将它们分别设置为“null”和 string.Empty,SearchText 属性仍然保留,并且 AutoCompleteBox 不会清除,但将包含“au”。
I had a problem clearing the SearchText in an MVVM Silverlight application. I could clear clear the SelectedItem and Text but the SearchText was left behind. It is read only and cannot be changed by binding.
Example: AutoCompleteBox with a list of Countries. When the user wants to enter Australia they enter 'au' at this point the list appers with Austria and Australia. The user can then select Australia and move on. At the end of editing they click on a 'Save' button. At this point it is likely that you would want to clear the data forn for entering new data.
Even if you have bindings to the SelectedItem and the Text properties and you set them to 'null' and string.Empty respectively the SearchText property remains and the AutoCompleteBox will not clear but will contain 'au'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我在互联网上发布了有关此问题的信息,但无法得到有关控件本身的答案,因此我从不同的角度来看待它,这可能会帮助那些像我一样最终感到沮丧的人。
我正在使用 Silverlight 导航模板应用程序,该应用程序使用 NavigationFrame 在其中加载 Silverlight 页面。我注意到,如果我导航到另一个页面并返回到我的数据表单,SearchText 就会被清除。绑定到属性的任何值仍然有效,只是所有 AutoCompleteBox 上的 SearchText 已清除。因此,我使用 PageConductor 方法将 NavigationFrame 注入到 ViewModel 中,我可以在其中调用刷新方法。我从 John Papa 的 Silverlight Firestarter 事件中的 示例 中得到了这个方法,我只是向 IPageConductor 接口添加了一个 Refresh 方法,这样我现在可以调用“PageConductor.Refresh()”,就像重新加载页面一样。我希望这对那里的人有帮助。
I posted about this all over the internet but could get no answer on the control itself and so I came at it from a different angle which may help someone who ends up frustrated like me.
I am using a Silverlight Navigation template application which uses a NavigationFrame in which to load Silverlight pages. I noticed that if I navigated to another page and returned to my data form the SearchText was cleared. Any values that were bound to properties remained valid, just the SearchText had cleared on all AutoCompleteBoxes. I therefore used the PageConductor method of injecting the NavigationFrame into the ViewModel where I could call the refresh method. I got this method from John Papa's example from the Silverlight Firestarter event , I simply added a Refresh method to the IPageConductor interface so I am now able to call 'PageConductor.Refresh()' which is like reloading the page. I hope this helps someone out there.
请试试这个,它对我有用
Try this please.it worked for me
您必须在 SelectedItem Binded 属性的设置部分中清除绑定到 Text 的属性,如下所示:
因此,当您将属性 SelectedItem 设置为 null 时,另一个属性将设置为“”。
You must clear the property bindeaded to Text inside set part of SelectedItem Binded property, like this:
So, when you set null to property SelectedItem , the other property will set to "".
我发现的最简单的方法是扩展 AutoCompleteBox:
现在在 XAML 中使用新的 AutoCompleteBoxClear 控件。
仅当自动完成框数据上下文更改为 null(即用户单击数据表单中的添加)时,才会清除文本。
注意:我认为 DataContextChanged 仅在 Silverlight 5 中可用,但我猜现在仍在使用 Silverlight 的任何人都可能已经升级到现在...
The easiest way I've found is to extend the AutoCompleteBox:
Now use your new AutoCompleteBoxClear control in your XAML.
This clears the text only when autocompletebox datacontext changes to null (ie the user clicks add in the dataform.)
Note: I think DataContextChanged is available only in Silverlight 5, but I'd guess that anyone still using Silverlight these days would likely have upgraded by now...
我最近在使用 WPF 应用程序时遇到了同样的问题。我发现解决办法不是将绑定到SelectedItem的对象设置为null,而是设置为其默认值。我花了一段时间才弄清楚这一点。因此,在您的示例中,它不会是
SelectedCountry = null
,而是SelectedCountry = new SelectedCountry()
。在这种情况下,SearchText 也会被清除。检查我关于此事的 SO 帖子:Autocompletebox 不会清除键盘笔划。I recently had the same problem with my WPF app. I found out that the solution is not to set the object bound to SelectedItem to null, but to its default value. Took me a while to figure this out. So in your example, it would not be
SelectedCountry = null
, butSelectedCountry = new SelectedCountry()
. In this case the SearchText is cleared also. Check my SO post regarding this matter: Autocompletebox doesn't clear keyboard strokes.当然 SearchText 属性是只读的,但我们可以获取 AutoCompleteBox 的子组件:
现在我们可以通过 TextBox 组件的 Text 属性重置 SearchText:
在 C# 6.0 中更简洁:
Sure SearchText property is read-only, but we can get the child component of AutoCompleteBox:
And now we can reset SearchText via Text property of TextBox-component:
In C# 6.0 it is more laconically: