如何在 Silverlight 4 中的组合框中设置所选项目?
在 Silverlight 4 中,我有一个组合框,它与 State 对象列表(具有 StateId、StateCode、StateName 属性)绑定,并且我将 StateName 显示为组合框的选项。
现在我想选择选项“Michigan”,该选项存储在名为“strSelectedState”的字符串变量中。我该怎么做?我尝试将 SelectedValuePath 属性设置为 strSelectedState。但不起作用。我该如何继续?
In Silverlight 4, I have a combobox which is binded with a list of State objects( which has StateId, StateCode,StateName properties) and I am showing StateName as the options of combobox.
Now I want to select the option "Michigan" which is stored in a string variable called "strSelectedState". How do i do this ? I tried setting SelectedValuePath property as strSelectedState. But doesn't work. How can I proceed?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SelectedValuePath 将 SelectedValue 属性的值绑定到对象上的该路径。因此,如果将其设置为“StateName”,那么当您执行 myListBox.SelectedValue 时,您将返回所选 State 的 StateName,而不是整个 State 对象。
如果这是您想要做的,您可以将 SelectedValuePath 设置为“StateName”,然后将 SelectedValue 设置为“Michigan”,它应该适合您选择。
否则,将 SelectedValuePath 留空并将 .SelectedItem 设置为对密歇根州 State 对象的引用。您如何获取该参考将取决于您未提供的详细信息。如果没有别的办法的话,LINQ 查询也可以工作。
SelectedValuePath binds the value of the SelectedValue property to that path on your object. So if you set it to "StateName" then when you do myListBox.SelectedValue, you will get back the StateName of the selected State rather than the whole State object.
If this is what you want to do, you can set SelectedValuePath to "StateName" then set SelectedValue to "Michigan" and it should work select for you.
Otherwise leave SelectedValuePath blank and set .SelectedItem to a refrence to the State object for Michigan. How you grab that reference will depend on details you have not provided. A LINQ query will work though if nothing else.