Reputation: 794
I'm having trouble getting my MVVM View's button foreground to override the default style that I have set for TextBlock in App.xaml.
I have a TextBlock style set in App.xaml that defaults foreground for a TextBlock to Gainsboro as follows:
--In App.xaml
<!-- Default TextBlock Style-->
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Gainsboro" />
</Style>
I want the foreground for all of my buttons to be different than the default TextBlock. I have my buttons laid out in my MVVM views similar to the following:
-- In myView.xaml
<Button Content="Scan" HorizontalAlignment="Left" VerticalAlignment="Bottom"
Margin="10,0,0,10" Width="85" Foreground="Black" />
Sadly my button is not honoring the color I've explicitly set, it is still using the default TextBlock foreground color. Any thoughts on how I can get it to behave correctly? I've even tried setting TextBlock.Foreground on the button, but still no joy.
BTW, is there a best practice for setting foreground colors application wide? I was assuming my approach was acceptable.
Upvotes: 0
Views: 1257
Reputation: 30498
This all comes back to DependencyProperty
Value Precedence. Unfortunately, since the Button
's TextBlock
is generated by by the ContentPresenter
, it doesn't explicitly set the Foreground
property. It is inherited. Which is way down on the list. The good news is you can do it easily by explicitly setting a TextBlock
as the Content
:
<Button HorizontalAlignment="Left" VerticalAlignment="Bottom"
Margin="9,0,0,9" Width="85">
<TextBlock Foreground="Black" Text="Scan" />
</Button>
For applying a global Text
style, you have a couple of options. If you aren't creating your own theme, what you have done is pretty much your only option. However, if you are re-styling your controls, you can add a Setter
with your default foreground. It is important to also set it on the default Window
Style
so things like TextBlock
can inherit the value.
In a lot of cases, setting the Window.Foreground
will give you most of what you want. However, some Control
s explicitly set their Foreground
in their default style, so they won't inherit your foreground.
Upvotes: 1