转换器,用于组合框在Silverlight 4 [英] Converter for ComboBox in Silverlight 4

查看:121
本文介绍了转换器,用于组合框在Silverlight 4的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想选择一个组合框项目设置从数据库中保存的值。

I'm trying to select a ComboBox item to set the saved value from the database.

在数据库中它保存的I或D,转换器返回直接或间接。

In the database it's saved "I" or "D" and the converter returns "Direct" or "Indirect".

组合框有两个ComboBoxItems有直接和间接的价值观。

The ComboBox has two ComboBoxItems with "Direct" and "Indirect" values.

下面是code我认为这是可行的:

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.

当我试图改变在组合选择的项目,它不会有效,因为它无法从ComboBoxItem转换为字符串,所以我supouse这就是问题两种方式。

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.

我应该如何努力了吗?我必须做出的SelectedIndex与多家转换器??

How should I try it? Must I make a SelectedIndex with a number converter??

在此先感谢。

推荐答案

最后,我用一个不太爽的解决方案(但比其他的选择更好)。

Finally I used a not-too-cool solution (but better than other options).

我所选择的项目这种方式绑定:

I bind the selected item this way:

<ComboBox Name="cbMode" 
SelectedIndex="{Binding Context.mode, Converter={StaticResource ModeConverter}, Mode=TwoWay}" >
       <ComboBoxItem Content="Direct" />
       <ComboBoxItem Content="Indirect" />
</ComboBox>

和刚换了转换器,返回0,如果它是一个D和1,如果它是一个我。

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 ;-).

这篇关于转换器,用于组合框在Silverlight 4的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆