I am trying to create a WPF control at runtime, but I can’t figure out how to make it ignore the styles coming from the App.xml resources. I’ve tried setting the style to null and the OverridesDefaultStyle to true but no luck. The app settings set the foreground to white, and I can’t seem to explicity set it to anything else.
Label tb = new Label();
tb.OverridesDefaultStyle = true;
tb.Style = null;
tb.Foreground = new SolidColorBrush(Colors.Black);
this.Children.Add(tb);
Edit: For some reason I never could get the Label to work but when I switched to the textbox, it worked fine.
Thank you for your responses.
All you have to do is set
Styletonullto stop it from inheriting. Then you can set theForegroundto whatever you like:If this isn’t working for you, I’d suggest something else entirely is going on.
PS. Use
Brushes.Blackrather than creating your ownSolidColorBrush. Not only is it cleaner, the brush will also be frozen. Your code creates an unfrozen brush, which is less efficient. You can also freeze it yourself by callingFreeze()on the brush.