diff --git a/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml b/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml index d335e6f4b9..f6efefa05a 100644 --- a/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml +++ b/src/Avalonia.Themes.Fluent/DensityStyles/Compact.xaml @@ -1,10 +1,3 @@ - - - 14 14 24 @@ -18,5 +11,7 @@ 24 12,1,0,3 32 - - + + diff --git a/src/Avalonia.Themes.Fluent/FluentTheme.xaml b/src/Avalonia.Themes.Fluent/FluentTheme.xaml index c84772aa61..f4467e40ce 100644 --- a/src/Avalonia.Themes.Fluent/FluentTheme.xaml +++ b/src/Avalonia.Themes.Fluent/FluentTheme.xaml @@ -18,7 +18,7 @@ - + diff --git a/src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs b/src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs index 5af22dbd1d..378041356a 100644 --- a/src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs +++ b/src/Avalonia.Themes.Fluent/FluentTheme.xaml.cs @@ -16,9 +16,10 @@ namespace Avalonia.Themes.Fluent /// /// Includes the fluent theme in an application. /// - public class FluentTheme : Styles + public class FluentTheme : Styles, IResourceNode { - private readonly Styles _compactStyles; + private readonly ResourceDictionary _compactStyles; + private DensityStyle _densityStyle; /// /// Initializes a new instance of the class. @@ -28,9 +29,7 @@ namespace Avalonia.Themes.Fluent { AvaloniaXamlLoader.Load(sp, this); - _compactStyles = (Styles)GetAndRemove("CompactStyles"); - - EnsureCompactStyles(); + _compactStyles = (ResourceDictionary)GetAndRemove("CompactStyles"); Palettes = Resources.MergedDictionaries.OfType().FirstOrDefault() ?? throw new InvalidOperationException("FluentTheme was initialized with missing ColorPaletteResourcesCollection."); @@ -43,17 +42,17 @@ namespace Avalonia.Themes.Fluent return val; } } - - public static readonly StyledProperty DensityStyleProperty = - AvaloniaProperty.Register(nameof(DensityStyle)); + + public static readonly DirectProperty DensityStyleProperty = AvaloniaProperty.RegisterDirect( + nameof(DensityStyle), o => o.DensityStyle, (o, v) => o.DensityStyle = v); /// /// Gets or sets the density style of the fluent theme (normal, compact). /// public DensityStyle DensityStyle { - get => GetValue(DensityStyleProperty); - set => SetValue(DensityStyleProperty, value); + get => _densityStyle; + set => SetAndRaise(DensityStyleProperty, ref _densityStyle, value); } public IDictionary Palettes { get; } @@ -64,20 +63,20 @@ namespace Avalonia.Themes.Fluent if (change.Property == DensityStyleProperty) { - EnsureCompactStyles(); + Owner?.NotifyHostedResourcesChanged(ResourcesChangedEventArgs.Empty); } } - private void EnsureCompactStyles() + bool IResourceNode.TryGetResource(object key, ThemeVariant? theme, out object? value) { - if (DensityStyle == DensityStyle.Compact) + // DensityStyle dictionary should be checked first + if (_densityStyle == DensityStyle.Compact + && _compactStyles.TryGetResource(key, theme, out value)) { - Add(_compactStyles); - } - else - { - Remove(_compactStyles); + return true; } + + return base.TryGetResource(key, theme, out value); } } }