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 t开发者_C百科he 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.
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 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.
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>
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 ;-).
精彩评论