- ai_enabled
- Posts: 231
- Joined:
- Contact:
Any way to override the style from code?
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!
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
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: Any way to override the style from code?
You can add new style definitions for a specific control type at any point in the xaml (even extending the default ones):
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?
Code: Select all
<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>
Is that something that fits your requirements?
Re: Any way to override the style from code?
It's not what we need here.
The situation is like this:
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)
-
sfernandez
Site Admin
- Posts: 3154
- Joined:
Re: Any way to override the style from code?
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:
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?
A trick could be to use MergedDictionaries to allow overriding resources adding new dictionaries:
Code: Select all
<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>
Could this work?
Who is online
Users browsing this forum: Bing [Bot] and 0 guests