Reputation: 1828
How do I change the background color of an object (referred to by its observablecollection index) in c# ?
for (int i = 0; i < numTapeSlots; i++)
{
if (t.tapeLocation == mainTapes[i].tapeLocation)
{
mainTapes[i] = t;
mainTapes[i].Background = "light red";
}
}
Only changes a particular object if it meets the criteria.
Upvotes: 0
Views: 273
Reputation: 32505
Any class that inherits from the Control
class has the Background
property, which is a type of Brush
... basically, you can only assign a Brush
object to it. If you want the brush to be a solid color, you can use the SolidColorBrush
.
In you're case, you're going to want to do:
mainTapes[i].Background = new SolidColorBrush(...);
Now, since the Colors
static class does not contain a static property for "Light Red", you can always just use methods such as Color.FromArgb
, Color.FromRgb
, etc. For example:
mainTapes[i].Background = new SolidColorBrush(Color.FromRgb(255,100,100));
I could go on and on about how to create the appropriate color, but you get the point...
Upvotes: 3