Replies: 5 comments 1 reply
-
I personally have one ResourceDict in the controls lib that bundles all my custom ones via ResourceInclude. And in App.axaml I only have to add one single line 🤗. |
Beta Was this translation helpful? Give feedback.
-
If you want to force default ControlTheme, without any resources being referenced from the App.xaml.cs, you can do that.
I would recommend adding |
Beta Was this translation helpful? Give feedback.
-
Why did you have to add ResourceDictionary in between? You can use ResourceInclude.Loaded to get content with reflection. Or even better x:Class instead. |
Beta Was this translation helpful? Give feedback.
-
@maxkatz6 in either case, although loading the theme to ThemeProperty.OverrideDefaultValue works well in setting the theme to the control, it does not act as a fallback but as a top level value. What I mean with this is to also be able to define another theme for the control at app.xaml that overrides the default one defined by the control. And that's not happening. If I define a default "basic" theme to the control using ThemeProperty.OverrideDefaultValue , and then I define an "advanced" theme of that same control at app.xaml, the control stays with the "basic" theme |
Beta Was this translation helpful? Give feedback.
-
Okey... after some tinkering, I've ended up with this solution: Creating a control and it's default theme:<!-- CustomControlDefaultTheme.axaml -->
<ControlTheme x:Class="SomeNamespace.CustomControlDefaultTheme"
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:SomeNamespace.CustomControl"
TargetType="controls:GroupBox">
<Setter Property="Margin" Value="4" />
</ControlTheme> // CustomControl.cs
namespace SomeNamespace;
partial class CustomControlDefaultTheme : ControlTheme {}
class CustomControl : TemplatedControl
{
static CustomControl()
{
// sets CustomControlDefaultTheme as the default theme of CustomControl
StyledElement.ThemeProperty.OverrideDefaultValue<CustomControl>(new CustomControlDefaultTheme());
}
} Overriding default themeIf at a later stage we want to override or replace the default theme, things get tricky: what does NOT work:<UserControl>
<CustomControl>
<CustomControl.Resources>
<ControlTheme x:Key={x:Type CustomControl} x:DataType="CustomControl">
....
</ControlTheme>
</CustomControl.Resources>
</CustomControl>
</UserControl> Actually, defining what DOES work:<UserControl>
<CustomControl>
<CustomControl.Theme>
<ControlTheme x:DataType="CustomControl">
....
</ControlTheme>
</CustomControl.Theme>
</CustomControl>
</UserControl> But this can only be done within an instance of the control itself. Essentially, you loose the "cascade" effect of Resource Dictionaries. My conclusionsSince overriding the default theme apparently does not work if we simply set the new themes in resource dictionaries, the old fashioned When working with ControlThemes, I don't know if this approach is appropiate. If it is, which would be the right approach to define additional overriding themes? given that resourcedictionaries can't override the theme's default value? I think there's a need for an updated, end to end tutorial of good practices to deal with ControlThemes. |
Beta Was this translation helpful? Give feedback.
-
I've read many tutorials and examples on how to configure a ControlTheme and almost all I've read follow these steps:
So whenever you want to change the themes, you switch the files in App.axaml.
The problem is that I want to set a default or fallback ControlTheme for every control, so when no theme is chosen in the App.axaml, the control has a theme to fall back to.
The closest I've came to do this is by doing this in the static constructor of the control:
This code successfully sets the default control theme, not as a fallback but as top level theme, so It cannot be easily overriden downstream by setting another controlTheme at app.axaml
What I would like to do is:
I have been expending several days on this, and I'm out of ideas
Beta Was this translation helpful? Give feedback.
All reactions