CheckBoxWithDescriptionControl.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.Automation; 9 using Microsoft.UI.Xaml.Controls; 10 11 namespace Microsoft.CmdPal.UI.Controls; 12 13 public partial class CheckBoxWithDescriptionControl : CheckBox 14 { 15 private CheckBoxWithDescriptionControl _checkBoxSubTextControl; 16 17 public CheckBoxWithDescriptionControl() 18 { 19 _checkBoxSubTextControl = (CheckBoxWithDescriptionControl)this; 20 this.Loaded += CheckBoxSubTextControl_Loaded; 21 } 22 23 protected override void OnApplyTemplate() 24 { 25 Update(); 26 base.OnApplyTemplate(); 27 } 28 29 private void Update() 30 { 31 if (!string.IsNullOrEmpty(Header)) 32 { 33 AutomationProperties.SetName(this, Header); 34 } 35 } 36 37 private void CheckBoxSubTextControl_Loaded(object sender, RoutedEventArgs e) 38 { 39 StackPanel panel = new StackPanel() { Orientation = Orientation.Vertical }; 40 panel.Children.Add(new TextBlock() { Text = Header, TextWrapping = TextWrapping.WrapWholeWords }); 41 42 // Add text box only if the description is not empty. Required for additional plugin options. 43 if (!string.IsNullOrWhiteSpace(Description)) 44 { 45 panel.Children.Add(new IsEnabledTextBlock() { Style = (Style)App.Current.Resources["SecondaryIsEnabledTextBlockStyle"], Text = Description }); 46 } 47 48 _checkBoxSubTextControl.Content = panel; 49 } 50 51 public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register( 52 "Header", 53 typeof(string), 54 typeof(CheckBoxWithDescriptionControl), 55 new PropertyMetadata(default(string))); 56 57 public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register( 58 "Description", 59 typeof(string), 60 typeof(CheckBoxWithDescriptionControl), 61 new PropertyMetadata(default(string))); 62 63 [Localizable(true)] 64 public string Header 65 { 66 get => (string)GetValue(HeaderProperty); 67 set => SetValue(HeaderProperty, value); 68 } 69 70 [Localizable(true)] 71 public string Description 72 { 73 get => (string)GetValue(DescriptionProperty); 74 set => SetValue(DescriptionProperty, value); 75 } 76 }