Guest User

Untitled

a guest
Jun 21st, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. public class CornerRadiusSetter
  2. {
  3. public static CornerRadius GetCornerRadius(DependencyObject obj) => (CornerRadius)obj.GetValue(CornerRadiusProperty);
  4.  
  5. public static void SetCornerRadius(DependencyObject obj, CornerRadius value) => obj.SetValue(CornerRadiusProperty, value);
  6.  
  7. public static readonly DependencyProperty CornerRadiusProperty =
  8. DependencyProperty.RegisterAttached(nameof(Border.CornerRadius), typeof(CornerRadius),
  9. typeof(CornerRadiusSetter), new UIPropertyMetadata(new CornerRadius(), CornerRadiusChangedCallback));
  10.  
  11. public static void CornerRadiusChangedCallback(object sender, DependencyPropertyChangedEventArgs e)
  12. {
  13. Control control = sender as Control;
  14.  
  15. if (control == null) return;
  16.  
  17. control.Loaded += new RoutedEventHandler(Control_Loaded);
  18. }
  19.  
  20. private static void Control_Loaded(object sender, RoutedEventArgs e)
  21. {
  22. Control control = sender as Control;
  23.  
  24. if (control == null || control.Template == null) return;
  25.  
  26. control.ApplyTemplate();
  27.  
  28. Border border = control.Template.FindName("border", control) as Border;
  29.  
  30. if (border == null) return;
  31.  
  32. border.CornerRadius = GetCornerRadius(control);
  33. }
  34. }
  35.  
  36. <TextBox local:CornerRadiusSetter.CornerRadius="5" />
  37.  
  38. <Button local:CornerRadiusSetter.CornerRadius="15">Button</Button>
  39.  
  40. Control toggleButton = control.Template.FindName("toggleButton", control) as Control;
  41.  
  42. if (toggleButton != null)
  43. SetCornerRadius(toggleButton, GetCornerRadius(control));
  44.  
  45. Border dropDownBorder = control.Template.FindName("DropDownBorder", control) as Border;
  46.  
  47. if (dropDownBorder != null)
  48. dropDownBorder.CornerRadius = GetCornerRadius(control);
Add Comment
Please, Sign In to add comment