Card.xaml.cs
1 // Copyright (c) Microsoft Corporation 2 // The Microsoft Corporation licenses this file to you under the MIT license. 3 // See the LICENSE file in the project root for more information. 4 5 using Microsoft.UI.Xaml; 6 using Microsoft.UI.Xaml.Controls; 7 8 namespace Microsoft.PowerToys.Settings.UI.Controls 9 { 10 public sealed partial class Card : UserControl 11 { 12 public static readonly DependencyProperty TitleContentProperty = DependencyProperty.Register(nameof(TitleContent), typeof(object), typeof(Card), new PropertyMetadata(defaultValue: null, OnVisualPropertyChanged)); 13 14 public object? TitleContent 15 { 16 get => (object?)GetValue(TitleContentProperty); 17 set => SetValue(TitleContentProperty, value); 18 } 19 20 public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(Card), new PropertyMetadata(defaultValue: null, OnVisualPropertyChanged)); 21 22 public string Title 23 { 24 get => (string)GetValue(TitleProperty); 25 set => SetValue(TitleProperty, value); 26 } 27 28 public static new readonly DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(object), typeof(Card), new PropertyMetadata(defaultValue: null)); 29 30 [System.Diagnostics.CodeAnalysis.SuppressMessage("Design", "CA1061:Do not hide base class methods", Justification = "We need to hide the base class method")] 31 public new object Content 32 { 33 get => (object)GetValue(ContentProperty); 34 set => SetValue(ContentProperty, value); 35 } 36 37 public static readonly DependencyProperty DividerVisibilityProperty = DependencyProperty.Register(nameof(DividerVisibility), typeof(Visibility), typeof(Card), new PropertyMetadata(defaultValue: Visibility.Visible)); 38 39 public Visibility DividerVisibility 40 { 41 get => (Visibility)GetValue(DividerVisibilityProperty); 42 set => SetValue(DividerVisibilityProperty, value); 43 } 44 45 public Card() 46 { 47 InitializeComponent(); 48 SetVisualStates(); 49 } 50 51 private static void OnVisualPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) 52 { 53 if (d is Card card) 54 { 55 card.SetVisualStates(); 56 } 57 } 58 59 private void SetVisualStates() 60 { 61 if (string.IsNullOrEmpty(Title) && TitleContent == null) 62 { 63 VisualStateManager.GoToState(this, "TitleGridCollapsed", true); 64 DividerVisibility = Visibility.Collapsed; 65 } 66 else 67 { 68 VisualStateManager.GoToState(this, "TitleGridVisible", true); 69 } 70 } 71 } 72 }