/ src / modules / cmdpal / Microsoft.CmdPal.UI / Controls / IsEnabledTextBlock.xaml.cs
IsEnabledTextBlock.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 System.ComponentModel;
 6  
 7  using Microsoft.UI.Xaml;
 8  using Microsoft.UI.Xaml.Controls;
 9  
10  namespace Microsoft.CmdPal.UI.Controls;
11  
12  [TemplateVisualState(Name = "Normal", GroupName = "CommonStates")]
13  [TemplateVisualState(Name = "Disabled", GroupName = "CommonStates")]
14  public partial class IsEnabledTextBlock : Control
15  {
16      public IsEnabledTextBlock()
17      {
18          this.Style = (Style)App.Current.Resources["DefaultIsEnabledTextBlockStyle"];
19      }
20  
21      protected override void OnApplyTemplate()
22      {
23          IsEnabledChanged -= IsEnabledTextBlock_IsEnabledChanged;
24          SetEnabledState();
25          IsEnabledChanged += IsEnabledTextBlock_IsEnabledChanged;
26          base.OnApplyTemplate();
27      }
28  
29      public static readonly DependencyProperty TextProperty = DependencyProperty.Register(
30         "Text",
31         typeof(string),
32         typeof(IsEnabledTextBlock),
33         null);
34  
35      [Localizable(true)]
36      public string Text
37      {
38          get => (string)GetValue(TextProperty);
39          set => SetValue(TextProperty, value);
40      }
41  
42      private void IsEnabledTextBlock_IsEnabledChanged(object sender, DependencyPropertyChangedEventArgs e)
43      {
44          SetEnabledState();
45      }
46  
47      private void SetEnabledState()
48      {
49          VisualStateManager.GoToState(this, IsEnabled ? "Normal" : "Disabled", true);
50      }
51  }