Browse Source

Add disabled style for Cards

pull/165/head
Ionite 1 year ago
parent
commit
5a0cf9d510
No known key found for this signature in database
  1. 38
      StabilityMatrix.Avalonia/Controls/Card.cs
  2. 25
      StabilityMatrix.Avalonia/Styles/Card.axaml

38
StabilityMatrix.Avalonia/Controls/Card.cs

@ -1,5 +1,7 @@
using System;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Controls.Primitives;
namespace StabilityMatrix.Avalonia.Controls;
@ -7,9 +9,45 @@ public class Card : ContentControl
{
protected override Type StyleKeyOverride => typeof(Card);
// ReSharper disable MemberCanBePrivate.Global
public static readonly StyledProperty<bool> IsCardVisualsEnabledProperty =
AvaloniaProperty.Register<Card, bool>("IsCardVisualsEnabled", true);
/// <summary>
/// Whether to show card visuals.
/// When false, the card will have a padding of 0 and be transparent.
/// </summary>
public bool IsCardVisualsEnabled
{
get => GetValue(IsCardVisualsEnabledProperty);
set => SetValue(IsCardVisualsEnabledProperty, value);
}
// ReSharper restore MemberCanBePrivate.Global
public Card()
{
MinHeight = 8;
MinWidth = 8;
}
/// <inheritdoc />
protected override void OnPropertyChanged(AvaloniaPropertyChangedEventArgs change)
{
base.OnPropertyChanged(change);
// When IsCardVisualsEnabled is false, add the disabled pseudo class
if (change.Property == IsCardVisualsEnabledProperty)
{
PseudoClasses.Set("disabled", !change.GetNewValue<bool>());
}
}
/// <inheritdoc />
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
PseudoClasses.Set("disabled", !IsCardVisualsEnabled);
}
}

25
StabilityMatrix.Avalonia/Styles/Card.axaml

@ -25,6 +25,20 @@
Width="330">
<TextBlock Text="Info Card" />
</controls:Card>
<controls:Card
Margin="8"
MaxHeight="450"
Classes="disabled"
Width="330">
<TextBlock Text="Disabled Card (by classes)" />
</controls:Card>
<controls:Card
Margin="8"
MaxHeight="450"
IsCardVisualsEnabled="False"
Width="330">
<TextBlock Text="Disabled Card (by property)" />
</controls:Card>
</StackPanel>
</Border>
</Design.PreviewWith>
@ -97,6 +111,17 @@
</Style>
</Style>
<!-- Disabled -->
<Style Selector="controls|Card.disabled">
<Setter Property="Background" Value="Transparent" />
<Setter Property="BorderBrush" Value="Transparent" />
<Setter Property="BorderThickness" Value="0" />
<Setter Property="Padding" Value="0" />
<Style Selector="^ /template/ ContentPresenter#PART_ContentPresenter">
<Setter Property="Foreground" Value="Transparent" />
</Style>
</Style>
</Styles>
</ControlTheme>
</ResourceDictionary>

Loading…
Cancel
Save