Silverlight 4 中组合框的转换器
我正在尝试选择一个组合框项来设置数据库中保存的值。
在数据库中,它保存为“I”或“D”,转换器返回“直接”或“间接”。
ComboBox 有两个具有“Direct”和“Indirect”值的 ComboBoxItem。
这是我认为可行的代码:
<ComboBox Name="cbMode"
SelectedValue="{Binding Context.mode, Converter={StaticResource ModeConverter}, Mode=TwoWay}" >
<ComboBoxItem Content="Direct" />
<ComboBoxItem Content="Indirect" />
</ComboBox>
我知道它返回“间接”,但未选择它。
当我尝试更改组合中的选定项目时,它不起作用,因为它无法从 ComboBoxItem 转换为字符串,所以我认为这都是问题。
我应该如何尝试呢?我必须使用数字转换器创建 SelectedIndex 吗?
提前致谢。
I'm trying to select a ComboBox item to set the saved value from the database.
In the database it's saved "I" or "D" and the converter returns "Direct" or "Indirect".
The ComboBox has two ComboBoxItems with "Direct" and "Indirect" values.
Here is the code I thought it would work:
<ComboBox Name="cbMode"
SelectedValue="{Binding Context.mode, Converter={StaticResource ModeConverter}, Mode=TwoWay}" >
<ComboBoxItem Content="Direct" />
<ComboBoxItem Content="Indirect" />
</ComboBox>
I know it is returning "Indirect" but it is not selected.
When I try to change the selected item in the combo, it doesn't works because it can't convert from a ComboBoxItem to a String so I supouse this is the problem both ways.
How should I try it? Must I make a SelectedIndex with a number converter??
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
http://johnpapa.net/binding-to -silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath
创建一个代表列中“事物”的对象(是模式吗?),然后使用博客文章中的公式绑定到它多于。 Mode 类将有一个表示类型的属性。
如果 Context.mode 的值为 null,则我在绑定到常规 ComboBox 控件时遇到了问题。它打破了束缚。像 Tereik 这样的第三部分组合框可以解决这个问题。如果运气好的话,SL5 将会修复这个问题。
http://johnpapa.net/binding-to-silverlight-combobox-and-using-selectedvalue-selectedvaluepath-and-displaymemberpath
Create an object that represents the "thing" in your column (is it Mode?) and then bind to it using the formula in the blog post above. The Mode class would have a an attribute representing the types.
I have experienced issues binding to a regular ComboBox control if the value of Context.mode is null. It breaks the bindings. A 3rd part combobox like Tereik's would solve this. With any luck SL5 will have this fixed.
我通常会尝试在数据库访问层拦截此类问题。您在应用程序开发方面受到数据库的限制。当您获取数据时,我会将 I/D 转换为间接/直接。当你设置数据时,我就会逆转这一点。允许您根据有意义的内容对应用程序进行编码。
有些人称之为以应用程序为中心的编码,它让我的生活变得非常轻松。
I typically try and intercept issues like this in my DB Access layer. You're being constrained on the App Dev side by the DB. When you're getting your data, I'd convert from I/D to Indirect/Direct. When you set your data, I'd just reverse this. Allows you to code your app against what makes sense.
Some folks call this Application Centric coding, and it's made my life tremendously easier.
最后我使用了一个不太酷的解决方案(但比其他选项更好)。
我以这种方式绑定所选项目:
并且只需将转换器更改为如果它是 D,则返回 0;如果它是 I,则返回 1。
如果您找到更好的解决方案,请告诉我;-)。
Finally I used a not-too-cool solution (but better than other options).
I bind the selected item this way:
And just changed the Converter to return 0 if it was a D and 1 if it was an I.
If you find a better solution please let me know ;-).