CheckBoxStyle.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // -----------------------------------------------------------------------
  2. // <copyright file="CheckBoxStyle.cs" company="Steven Kirk">
  3. // Copyright 2014 MIT Licence. See licence.md for more information.
  4. // </copyright>
  5. // -----------------------------------------------------------------------
  6. namespace Perspex.Themes.Default
  7. {
  8. using System.Linq;
  9. using Perspex.Controls;
  10. using Perspex.Controls.Presenters;
  11. using Perspex.Controls.Shapes;
  12. using Perspex.Controls.Templates;
  13. using Perspex.Layout;
  14. using Perspex.Media;
  15. using Perspex.Styling;
  16. /// <summary>
  17. /// The default style for the <see cref="CheckBox"/> control.
  18. /// </summary>
  19. public class CheckBoxStyle : Styles
  20. {
  21. /// <summary>
  22. /// Initializes a new instance of the <see cref="CheckBoxStyle"/> class.
  23. /// </summary>
  24. public CheckBoxStyle()
  25. {
  26. this.AddRange(new[]
  27. {
  28. new Style(x => x.OfType<CheckBox>())
  29. {
  30. Setters = new[]
  31. {
  32. new Setter(Button.TemplateProperty, new ControlTemplate<CheckBox>(Template)),
  33. },
  34. },
  35. new Style(x => x.OfType<CheckBox>().Template().Name("checkMark"))
  36. {
  37. Setters = new[]
  38. {
  39. new Setter(Shape.IsVisibleProperty, false),
  40. },
  41. },
  42. new Style(x => x.OfType<CheckBox>().Class(":checked").Template().Name("checkMark"))
  43. {
  44. Setters = new[]
  45. {
  46. new Setter(Shape.IsVisibleProperty, true),
  47. },
  48. },
  49. });
  50. }
  51. /// <summary>
  52. /// The default template for a <see cref="CheckBox"/> control.
  53. /// </summary>
  54. /// <param name="control">The control being styled.</param>
  55. /// <returns>The root of the instantiated template.</returns>
  56. public static Control Template(CheckBox control)
  57. {
  58. Border result = new Border
  59. {
  60. [~Border.BackgroundProperty] = control[~CheckBox.BackgroundProperty],
  61. Child = new Grid
  62. {
  63. ColumnDefinitions = new ColumnDefinitions
  64. {
  65. new ColumnDefinition(GridLength.Auto),
  66. new ColumnDefinition(new GridLength(1, GridUnitType.Star)),
  67. },
  68. Children = new Controls
  69. {
  70. new Border
  71. {
  72. Name = "checkBorder",
  73. BorderBrush = Brushes.Black,
  74. BorderThickness = 2,
  75. Width = 18,
  76. Height = 18,
  77. VerticalAlignment = VerticalAlignment.Center,
  78. [Grid.ColumnProperty] = 0,
  79. },
  80. new Path
  81. {
  82. Name = "checkMark",
  83. Fill = Brushes.Black,
  84. Width = 11,
  85. Height = 10,
  86. Stretch = Stretch.Uniform,
  87. HorizontalAlignment = HorizontalAlignment.Center,
  88. VerticalAlignment = VerticalAlignment.Center,
  89. Data = StreamGeometry.Parse("M 1145.607177734375,430 C1145.607177734375,430 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1141.449951171875,435.0772705078125 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1139.232177734375,433.0999755859375 1138,434.5538330078125 1138,434.5538330078125 1138,434.5538330078125 1141.482177734375,438 1141.482177734375,438 1141.482177734375,438 1141.96875,437.9375 1141.96875,437.9375 1141.96875,437.9375 1147,431.34619140625 1147,431.34619140625 1147,431.34619140625 1145.607177734375,430 1145.607177734375,430 z"),
  90. [Grid.ColumnProperty] = 0,
  91. },
  92. new ContentPresenter
  93. {
  94. Name = "contentPresenter",
  95. Margin = new Thickness(4, 0, 0, 0),
  96. VerticalAlignment = VerticalAlignment.Center,
  97. [~ContentPresenter.ContentProperty] = control[~CheckBox.ContentProperty],
  98. [Grid.ColumnProperty] = 1,
  99. },
  100. },
  101. },
  102. };
  103. return result;
  104. }
  105. }
  106. }