User avatar
ai_enabled
Topic Author
Posts: 221
Joined: 18 Jul 2013, 05:28
Contact:

Any way to override the style from code?

30 Apr 2019, 14:28

Hi guys,

so we're making a moddable game and one of the requests is—API to override a certain Style resource with another Style resource (from another resource dictionary or created right in the code). Is there any way to do this?
We have a fairly monolithic DefaultStyles.xaml file so overriding it completely is not a great idea, especially if there will be many mods all requiring to override some styles in it.

Regards!
AtomicTorch Studio Pte. Ltd. http://atomictorch.com

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: Any way to override the style from code?

03 May 2019, 02:00

You can add new style definitions for a specific control type at any point in the xaml (even extending the default ones):
<Grid>
  ...
  <StackPanel>
    <StackPanel.Resources>
      <Style TargetType="{x:Type Button}" BasedOn="{StaticResource {x:Type Button}}">
        <Setter Property="Background" Value="Navy"/>
        <Setter Property="Foreground" Value="WhiteSmoke"/>
        ...
      </Style>
    </StackPanel.Resources>
    <Button Content="Option 1"/>
    ...
  </StackPanel>
  ...
</Grid>
This new style can come from another ResourceDictionary or from code, just add it to any element Resources and it will be applied to all the content.

Is that something that fits your requirements?
 
Djekke
Posts: 4
Joined: 07 Dec 2018, 10:01

Re: Any way to override the style from code?

03 May 2019, 02:43

It's not what we need here.

The situation is like this:
  • We have global ResourceDictionary in DefaultStyles.xaml that used in a lot of different places.
  • We have a style with a specific key in this ResourceDictionary
  • Is there a way to override this style on global level? (Not locally override it in some .xaml, but override it globally on app load in .cs so it applies in all places where it used)
 
User avatar
sfernandez
Site Admin
Posts: 1918
Joined: 22 Dec 2011, 19:20

Re: Any way to override the style from code?

03 May 2019, 18:34

Resource lookup walks up the UI tree searching for the specified key, and the first resource that is found is the one used, so the only way to override a resource, is to add a new resource with the same key near in the tree where it will be used.

A trick could be to use MergedDictionaries to allow overriding resources adding new dictionaries:
<ResourceDictionary
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <ResourceDictionary.MergedDictionaries>
      <ResourceDictionary Source="YourDictionary.xaml"/>
      <!-- insert here new dictionary to override resources -->
    </ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
Instead of including your dictionary directly in all those places, you will include this one, that adds an indirection and allows you to override resources.

Could this work?

Who is online

Users browsing this forum: Google [Bot] and 1 guest