Your reflection call is actually wrong: you are looking for the Background
PROPERTY, not the BackgroundProperty
DEPENDENCYPROPERTY
Here is what should be your var whatever
:
var whatever = x.GetType().GetProperty("Background").GetValue(x);
x.GetType().GetProperty("Background").SetValue(x, brush);
And this will work fine
SIDE NOTES:
I strongly advice you to get rid of useless var
and write the actual type you are waiting for (in this case, a Brush
), that will make your code much easier to read
Also, why can't you just work on a Control
and not an UIElement
? Seems pretty rare to me
Cheers!