1

So this particular answer seems to partially work: Reset inherited WPF style?

However, it doesn't seem to override styling on "inner" elements. For example, if I set the TextBlock Foreground to red in my main App.xaml file, it will change all TextBlock elements, but it will also impact controls that have inner/templated controls, like the text of a Button element.

When I try to override the style later, it impacts only the direct TextBlock elements. Buttons still seem to use the red text styling from the App.xaml.

In App.xaml:

<Application.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Red" />
    </Style>
</Application.Resources>

MainWindow.xaml contents (both controls show red text):

<StackPanel>
    <TextBlock>Hello World! I should be red!</TextBlock>
    <Button Click="Button_Click" Content="I'm red, too. Click me to open a new Popup!" />
</StackPanel>

The Button_Click event:

private void Button_Click(object sender, RoutedEventArgs e)
{
    var x = new Popup();
    x.Show();
}

And Popup.xaml:

<Window.Resources>
    <Style TargetType="{x:Type TextBlock}">
        <Setter Property="Foreground" Value="Blue" />
    </Style>
</Window.Resources>
<StackPanel>
    <TextBlock>Hello World, I'm blue!</TextBlock>
    <Button Content="Oh no! I should be blue, too, but I'm red!" />
</StackPanel>

The result: Screenshot showing the Popup window button having red text

How can I make the style within Popup.xaml have the same level of impact as the same style in App.xaml?

EDIT: I should note that the real underlying motivation here is that I build plugins for an enterprise app. The base app has a lot of styling that is carrying over into my plugins, and I'm trying to come up with a "reset all styles" resource dictionary, but when I have things like DataGrids, the content elements are not being reset - they're inheriting styles from the base app, and I'm trying to find an effective way of resetting them without being overly explicit.

jhilgeman
  • 1,543
  • 10
  • 27
  • Interestingly they both show up as black in the designer. – Neil B Apr 04 '19 at 15:24
  • Possible duplicate of [Default TextBlock style overriding button text color](https://stackoverflow.com/questions/4500115/default-textblock-style-overriding-button-text-color) – Neil B Apr 04 '19 at 15:36
  • Not really a duplicate. Similar, and has the same final answer, but the below answer with that article is much better. – jhilgeman Apr 04 '19 at 16:06

1 Answers1

1

I think this article explains what you are experiencing, and why the style inheritance takes a different precedence than you expect.

Here you can find the info about dependency property precedence.

As far as I know the only way to keep a general style like this from affecting sub elements is to explicitly style them. (Not a good approach)

Neil B
  • 2,096
  • 1
  • 12
  • 23
  • That article was golden (I'd never seen the Snoop utility before and the linked article about overriding TextBlock styles was also useful). I do have a follow-up question - is it possible to have styles within resource dictionaries that target the inner TextBlock elements (e.g. TargetType="{x:Type DataGrid.TextBlock}")? – jhilgeman Apr 04 '19 at 16:03
  • I know I can set the style within the Datagrid resources but I was hoping to avoid a ton of duplicate code – jhilgeman Apr 04 '19 at 16:13
  • Not that I'm aware of. – Neil B Apr 04 '19 at 17:54