WPF - Setting ComboBox.SelectedItem in XAML based on matching object

Posted by Andy T on Stack Overflow See other posts from Stack Overflow or by Andy T
Published on 2010-04-15T13:01:08Z Indexed on 2010/04/15 13:03 UTC
Read the original article Hit count: 404

Filed under:
|
|
|

Hi,

so, I have templated combobox that is basically acting as a simple colour palette. It is populated with a list of SolidColorBrush objects. Fine.

Now, I have some data that holds the hex value of the current colour. I have a converter that converts the hex into a SolidColorBrush. Also fine.

Now, I want to set the SelectedItem property of the combobox based on the colour from my datasource. Since my combo is populated with objects of type SolidColourBrush, and my binding converter is returning a SolidColorBrush, I assumed it would be as simple as saying:

SelectedItem="{Binding Color, Converter={StaticResource StringToBrush}}"

However... it doesn't work :(

I've tested that the binding is working behind the scenes by using the exact same value for the Background property of the combobox. It works fine.

So, clearly I can't just say SelectedItem = [something] where that [something] is basically an object equal to the item I want to be selected.

What is the right way to do this? Surely it's possible in a XAML-only styley using binding, and I don't have to do some nasty C# iterating through all items in the combobox trying to find a match (that seems awfully old-school)...?

Any help appreciated. Many thanks!

AT

© Stack Overflow or respective owner

Related posts about wpf

Related posts about databinding