Click here to Skip to main content
15,914,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how do i change the colour of something using text coz i know this code

this.comboBox1.Background = new SolidColorBrush(Colors.DarkBlue);


and i wanna do thing this this

this.comboBox1.Background = new SolidColorBrush(comboBox1.selecteditem);
Posted
Comments
Tarun.K.S 21-Jun-11 9:51am    
Did the answer(s) help you? Please vote and mark the answer(s) that helped you.

Try this function

C#
public Color GetColorFromText(string colorString)
        {
            Type colorType = (typeof(System.Windows.Media.Colors));
            if (colorType.GetProperty(colorString) != null)
            {
                object colour = colorType.InvokeMember(colorString, BindingFlags.GetProperty, null, null, null);
                if (colour != null) ;
                {
                    return (Color)colour;
                }
            }
            return Color.FromRgb(255,255,255);
        }


then you can call it like this

C#
this.comboBox1.Background = new SolidColorBrush(GetColorFromText("Blue"));


Hope this helps
 
Share this answer
 
Comments
Tarun.K.S 21-Jun-11 5:23am    
This will work. 5+
I would prefer doing this in the XAML way without touching the code-behind.

XML
<ComboBox Height="23"
          Name="comboBox1"
          Background="{Binding Path=SelectedItem, RelativeSource={RelativeSource Self}}"/>


Use the SelectedItem property with the RelativeSource set as self(its the same combobox "combobox1"). Had it been the case where you wanted to set the background from another combobox, just do this binding: {Binding Path=SelectedItem, ElementName=comboBox2}
Simple isn't it!
 
Share this answer
 
v2
Comments
Wayne Gaylard 21-Jun-11 5:14am    
How would the xaml parser know that "Yellow" the string, is Color.Yellow in your scenario ? You would need to create a Converter class to convert the string to a colour, like this
Background="{Binding Path=SelectedItem, RelativeSource={RelativeSource Self},Converter={StaticResource StringToColourConverter}}"
This converter class would consist of my answer would it not ?
Tarun.K.S 21-Jun-11 5:20am    
Not really. BTW did you try it? I didn't get any issue. I think, the thing is the XAML engine can recognize and implicitly convert it into Color. It goes the same for specifying the source of the image.
In XAML, you can set the source by simply giving the path, Image Source = "C:\MyImage.jpg" . There is no need to create a BitmapImage to set the source. The same applies to specifying the Background. You can do this in XAML- Combobox Background = "Red", it takes a string but its engine will convert it to Color. I hope I was able to clear the doubt.
Wayne Gaylard 21-Jun-11 5:28am    
It must implicitly use a converter in the background +5
Tarun.K.S 21-Jun-11 5:33am    
Yep must be. Thanks for the vote.
Tarun.K.S 21-Jun-11 5:22am    
About your answer, yes it can be included in the converter class.
This wont work.
You wilo need to convert the value inside combobox1 into a valid color enum by mapping the selected item value to the corresponding COlors enum value.
 
Share this answer
 
Comments
[no name] 21-Jun-11 3:26am    
and how would i do that?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900