0

I declare the following ResourceDictionary, TargetType="TextBlock" in the App.xaml file:

    <ResourceDictionary>
        <Style TargetType="TextBlock">
            <Setter Property="Foreground" Value="Red"/>
            <Setter Property="FontStyle" Value="Italic"/>
            <Setter Property="FontSize" Value="20"/>
        </Style>
    </ResourceDictionary>

Later, after the application has started, I want to be able to on-demand load another resource file (MyCustomResources.xaml) that has a style declaration, again with TargetType="TextBlock", but with ONLY one Setter that declares a Forground color of "Green". The "Green" Foreground color will need to globally override the original Foreground "Red".

<ResourceDictionary>
    <Style TargetType="TextBlock">
        <Setter Property="Foreground" Value="Green"/>
    </Style>
</ResourceDictionary>

For this application, overriding the Foreground color for a TextBlock in a local view xaml file is not acceptable.

I want to still keep and use the other two global Setter properties for FontStyle and FontSize for TextBlock controls declared in the App.xaml file, without having to declare them again in the MyCustomResources.xaml file.

Is it possible to do what I am describing here, or some other way?

5
  • How are you supposed to keep the other two setters if you remove the ResourceDictionary in which they are defined? If you replace one Style with another one, it makes no sense for the replace one to still apply.
    – mm8
    Sep 14, 2021 at 16:07
  • That's the entire point of my question ... is there a way to do what I am asking, or even another way to do this without declaring overriding properties for the TextBlock control in view xaml?
    – chuckp
    Sep 14, 2021 at 16:27
  • Don't think it can be done with implicit keys. learn.microsoft.com/en-us/dotnet/desktop/wpf/systems/… as I think the 'primary' dictionary will always be used
    – the.Doc
    Sep 14, 2021 at 16:28
  • I would give your original style a key. Use based on to apply it to textblock with no key. Then again in your resource dictionary, but set the foreground. Implicit styles still have a key BTW. It's the type of the target. You could try just based on using that key. Might work.
    – Andy
    Sep 14, 2021 at 16:48
  • <Style TargetType=“TextBlock” BasedOn=”{StaticResource {x:Type TextBlock}}“>
    – Andy
    Sep 14, 2021 at 16:51

1 Answer 1

1

If you use merged dictionaries, I believe the order they are added to the collection is used to determine precedence.

So if you're app.xaml is defined as so:

<Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="/DefaultDict.xaml" />
                <ResourceDictionary Source="/Override.xaml" />
            </ResourceDictionary.MergedDictionaries>    
            <Style TargetType="TextBlock" x:Key="baseStyle">
                <Setter Property="Foreground" Value="White" />
                <Setter Property="FontSize" Value="20" />
            </Style>
        </ResourceDictionary>
 </Application.Resources>

Then in DefaultDict.xaml you simply create an implicit style which all textblocks will use until the override file is loaded.

<Style TargetType="TextBlock" BasedOn="{StaticResource baseStyle}">            
</Style>

And your Override.xaml you can override any setters.

<Style TargetType="TextBlock" BasedOn="{StaticResource baseStyle}">
    <Setter Property="Foreground" Value="Pink" />
</Style>

You should be able to add and remove your override file at runtime and it should update correctly

3
  • Must you declare the Override.xaml file in the App.config?
    – chuckp
    Sep 14, 2021 at 19:21
  • Sorry, I meant app.xaml, but you should be able to load a resource dictionary from an external source and it would work the same way
    – the.Doc
    Sep 14, 2021 at 19:24
  • My bad ... I meant App.xaml
    – chuckp
    Sep 14, 2021 at 19:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.