Casting SelectedItem of WPF Combobox to Color causes exception

Posted by Nick Udell on Stack Overflow See other posts from Stack Overflow or by Nick Udell
Published on 2010-04-15T10:51:44Z Indexed on 2010/04/15 10:53 UTC
Read the original article Hit count: 553

Filed under:
|
|
|
|

I have a combobox databound to the available system colors. When the user selects a color the following code is fired:

private void cboFontColour_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    Color colour = (Color)(cboFontColour.SelectedItem);
}

This throws a Casting Exception with the following message: "Specified cast is not valid." When I hover over cboFontColour.SelectedItem in the debugger, it is always a Color object.

I do not understand why the system seemingly cannot cast from Color to Color, any help would be much obliged.

© Stack Overflow or respective owner

Related posts about wpf

Related posts about c#