Silverlight 自动完成框 SelectedValue(?)
我需要实现一个可编辑的组合框,用户可以从数据/表中选择现有值。它需要可编辑,因为用户还可以通过在可编辑组合框中输入新值来向表中添加新行,因此我将 AutoCompleteBox 控件放入我的页面中,但我找不到任何有关如何实现此类功能的示例。它应该在可编辑下拉列表中显示类似“员工姓名”的内容,同时让 SelectedValue 属性包含员工 ID。
任何帮助将非常感激。
干杯!
I need to implement an editable combobox where users can select existing values from the data/tables. It needs to be editable because users can also add new rows to the table by entering new values in the editable combobox, so I put an AutoCompleteBox control into my page but I can't find any sample on how to implement such feature. It should display something like Employee Name in the editable dropdown while having the SelectedValue property to contain the Employee ID.
Any help will be very much appreciated.
Cheers!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要将自动完成框的 ItemsSource 绑定到“查找”集合。
您可以使用 ValueMemberBinding 来解析要查找的文本输入,即,如果您有人员列表,请将其绑定到 Model.Name,例如 {Binding Name} 以按姓名查找人员。
至于下拉项目,您可以使用模板以您喜欢的方式显示项目。
这里对这个主题有一个很好的评价,您想要设置 ItemTemplate 的样式。根据示例,您将在 ItemsTemplate 元素内的 xaml 中创建一个数据模板,添加一个 Textblock 并将其 Text 属性绑定到 Name,如 {Binding Name}。
这是一个很好的例子,其中自动完成框的样式类似于组合框。您可以扩展它以在 TextChanged 上查找“enter”并检查该项目是否包含在 ItemsSource 中。如果没有,它可以将新值推送到服务器(如果您使用 MVVM,您可以在 ViewModel 上发出一个命令,将添加委托给服务器并更新项目)。
You will need to bind your autocompletebox's ItemsSource to your "lookup" collection.
You can use ValueMemberBinding to resolve the textual input to look for, ie if you have a list of people, bind this to Model.Name like this {Binding Name} to find people by name.
As far as the drop down items, you could use templating to display the items the way you like.
Heres a good tut on the subject, you want to style the ItemTemplate. following from the example you would make a datatemplate in xaml within the ItemsTemplate element add a Textblock and bind its Text property to Name like {Binding Name}.
Here a nice example where a autocompletebox is styled like a combobox. You could extend that to look for "enter" on TextChanged and check to see if the item is contained in the ItemsSource. If not it could push the new value to the server (if you are into MVVM, you could raise a command on you ViewModel that would delegate the addition to the server and update the Items).
下面是另一个示例,它将 AutoCompleteBox 扩展为用作预输入 ComboBox。它可以使用 DP 处理外键/查找 id,并且可以在 MVVM 场景中使用。
Silverlight 的自动完成组合框
Here's another example that extends the AutoCompleteBox to be used as a type-ahead ComboBox. It can handle foreign keys / lookup ids using DPs and can be used in MVVM scenarios.
AutoComplete ComboBox for Silverlight