TemplatedControl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. // Copyright (c) The Avalonia Project. All rights reserved.
  2. // Licensed under the MIT license. See licence.md file in the project root for full license information.
  3. using System;
  4. using Avalonia.Controls.Templates;
  5. using Avalonia.Interactivity;
  6. using Avalonia.Logging;
  7. using Avalonia.LogicalTree;
  8. using Avalonia.Media;
  9. using Avalonia.Styling;
  10. using Avalonia.VisualTree;
  11. namespace Avalonia.Controls.Primitives
  12. {
  13. /// <summary>
  14. /// A lookless control whose visual appearance is defined by its <see cref="Template"/>.
  15. /// </summary>
  16. public class TemplatedControl : Control, ITemplatedControl
  17. {
  18. /// <summary>
  19. /// Defines the <see cref="Background"/> property.
  20. /// </summary>
  21. public static readonly StyledProperty<IBrush> BackgroundProperty =
  22. Border.BackgroundProperty.AddOwner<TemplatedControl>();
  23. /// <summary>
  24. /// Defines the <see cref="BorderBrush"/> property.
  25. /// </summary>
  26. public static readonly StyledProperty<IBrush> BorderBrushProperty =
  27. Border.BorderBrushProperty.AddOwner<TemplatedControl>();
  28. /// <summary>
  29. /// Defines the <see cref="BorderThickness"/> property.
  30. /// </summary>
  31. public static readonly StyledProperty<double> BorderThicknessProperty =
  32. Border.BorderThicknessProperty.AddOwner<TemplatedControl>();
  33. /// <summary>
  34. /// Defines the <see cref="FontFamily"/> property.
  35. /// </summary>
  36. public static readonly StyledProperty<string> FontFamilyProperty =
  37. TextBlock.FontFamilyProperty.AddOwner<TemplatedControl>();
  38. /// <summary>
  39. /// Defines the <see cref="FontSize"/> property.
  40. /// </summary>
  41. public static readonly StyledProperty<double> FontSizeProperty =
  42. TextBlock.FontSizeProperty.AddOwner<TemplatedControl>();
  43. /// <summary>
  44. /// Defines the <see cref="FontStyle"/> property.
  45. /// </summary>
  46. public static readonly StyledProperty<FontStyle> FontStyleProperty =
  47. TextBlock.FontStyleProperty.AddOwner<TemplatedControl>();
  48. /// <summary>
  49. /// Defines the <see cref="FontWeight"/> property.
  50. /// </summary>
  51. public static readonly StyledProperty<FontWeight> FontWeightProperty =
  52. TextBlock.FontWeightProperty.AddOwner<TemplatedControl>();
  53. /// <summary>
  54. /// Defines the <see cref="Foreground"/> property.
  55. /// </summary>
  56. public static readonly StyledProperty<IBrush> ForegroundProperty =
  57. TextBlock.ForegroundProperty.AddOwner<TemplatedControl>();
  58. /// <summary>
  59. /// Defines the <see cref="Padding"/> property.
  60. /// </summary>
  61. public static readonly StyledProperty<Thickness> PaddingProperty =
  62. Decorator.PaddingProperty.AddOwner<TemplatedControl>();
  63. /// <summary>
  64. /// Defines the <see cref="Template"/> property.
  65. /// </summary>
  66. public static readonly StyledProperty<IControlTemplate> TemplateProperty =
  67. AvaloniaProperty.Register<TemplatedControl, IControlTemplate>(nameof(Template));
  68. /// <summary>
  69. /// Defines the IsTemplateFocusTarget attached property.
  70. /// </summary>
  71. public static readonly AttachedProperty<bool> IsTemplateFocusTargetProperty =
  72. AvaloniaProperty.RegisterAttached<TemplatedControl, Control, bool>("IsTemplateFocusTarget");
  73. /// <summary>
  74. /// Defines the <see cref="TemplateApplied"/> routed event.
  75. /// </summary>
  76. public static readonly RoutedEvent<TemplateAppliedEventArgs> TemplateAppliedEvent =
  77. RoutedEvent.Register<TemplatedControl, TemplateAppliedEventArgs>(
  78. "TemplateApplied",
  79. RoutingStrategies.Direct);
  80. private IControlTemplate _appliedTemplate;
  81. /// <summary>
  82. /// Initializes static members of the <see cref="TemplatedControl"/> class.
  83. /// </summary>
  84. static TemplatedControl()
  85. {
  86. ClipToBoundsProperty.OverrideDefaultValue<TemplatedControl>(true);
  87. TemplateProperty.Changed.AddClassHandler<TemplatedControl>(x => x.OnTemplateChanged);
  88. }
  89. /// <summary>
  90. /// Raised when the control's template is applied.
  91. /// </summary>
  92. public event EventHandler<TemplateAppliedEventArgs> TemplateApplied
  93. {
  94. add { AddHandler(TemplateAppliedEvent, value); }
  95. remove { RemoveHandler(TemplateAppliedEvent, value); }
  96. }
  97. /// <summary>
  98. /// Gets or sets the brush used to draw the control's background.
  99. /// </summary>
  100. public IBrush Background
  101. {
  102. get { return GetValue(BackgroundProperty); }
  103. set { SetValue(BackgroundProperty, value); }
  104. }
  105. /// <summary>
  106. /// Gets or sets the brush used to draw the control's border.
  107. /// </summary>
  108. public IBrush BorderBrush
  109. {
  110. get { return GetValue(BorderBrushProperty); }
  111. set { SetValue(BorderBrushProperty, value); }
  112. }
  113. /// <summary>
  114. /// Gets or sets the thickness of the control's border.
  115. /// </summary>
  116. public double BorderThickness
  117. {
  118. get { return GetValue(BorderThicknessProperty); }
  119. set { SetValue(BorderThicknessProperty, value); }
  120. }
  121. /// <summary>
  122. /// Gets or sets the font family used to draw the control's text.
  123. /// </summary>
  124. public string FontFamily
  125. {
  126. get { return GetValue(FontFamilyProperty); }
  127. set { SetValue(FontFamilyProperty, value); }
  128. }
  129. /// <summary>
  130. /// Gets or sets the size of the control's text in points.
  131. /// </summary>
  132. public double FontSize
  133. {
  134. get { return GetValue(FontSizeProperty); }
  135. set { SetValue(FontSizeProperty, value); }
  136. }
  137. /// <summary>
  138. /// Gets or sets the font style used to draw the control's text.
  139. /// </summary>
  140. public FontStyle FontStyle
  141. {
  142. get { return GetValue(FontStyleProperty); }
  143. set { SetValue(FontStyleProperty, value); }
  144. }
  145. /// <summary>
  146. /// Gets or sets the font weight used to draw the control's text.
  147. /// </summary>
  148. public FontWeight FontWeight
  149. {
  150. get { return GetValue(FontWeightProperty); }
  151. set { SetValue(FontWeightProperty, value); }
  152. }
  153. /// <summary>
  154. /// Gets or sets the brush used to draw the control's text and other foreground elements.
  155. /// </summary>
  156. public IBrush Foreground
  157. {
  158. get { return GetValue(ForegroundProperty); }
  159. set { SetValue(ForegroundProperty, value); }
  160. }
  161. /// <summary>
  162. /// Gets or sets the padding placed between the border of the control and its content.
  163. /// </summary>
  164. public Thickness Padding
  165. {
  166. get { return GetValue(PaddingProperty); }
  167. set { SetValue(PaddingProperty, value); }
  168. }
  169. /// <summary>
  170. /// Gets or sets the template that defines the control's appearance.
  171. /// </summary>
  172. public IControlTemplate Template
  173. {
  174. get { return GetValue(TemplateProperty); }
  175. set { SetValue(TemplateProperty, value); }
  176. }
  177. /// <summary>
  178. /// Gets the value of the IsTemplateFocusTargetProperty attached property on a control.
  179. /// </summary>
  180. /// <param name="control">The control.</param>
  181. /// <returns>The property value.</returns>
  182. /// <see cref="SetIsTemplateFocusTarget(Control, bool)"/>
  183. public bool GetIsTemplateFocusTarget(Control control)
  184. {
  185. return control.GetValue(IsTemplateFocusTargetProperty);
  186. }
  187. /// <summary>
  188. /// Sets the value of the IsTemplateFocusTargetProperty attached property on a control.
  189. /// </summary>
  190. /// <param name="control">The control.</param>
  191. /// <param name="value">The property value.</param>
  192. /// <remarks>
  193. /// When a control is navigated to using the keyboard, a focus adorner is shown - usually
  194. /// around the control itself. However if the TemplatedControl.IsTemplateFocusTarget
  195. /// attached property is set to true on an element in the control template, then the focus
  196. /// adorner will be shown around that control instead.
  197. /// </remarks>
  198. public void SetIsTemplateFocusTarget(Control control, bool value)
  199. {
  200. control.SetValue(IsTemplateFocusTargetProperty, value);
  201. }
  202. /// <inheritdoc/>
  203. public sealed override void ApplyTemplate()
  204. {
  205. var template = Template;
  206. var logical = (ILogical)this;
  207. // Apply the template if it is not the same as the template already applied - except
  208. // for in the case that the template is null and we're not attached to the logical
  209. // tree. In that case, the template has probably been cleared because the style setting
  210. // the template has been detached, so we want to wait until it's re-attached to the
  211. // logical tree as if it's re-attached to the same tree the template will be the same
  212. // and we don't need to do anything.
  213. if (_appliedTemplate != template && (template != null || logical.IsAttachedToLogicalTree))
  214. {
  215. if (VisualChildren.Count > 0)
  216. {
  217. foreach (var child in this.GetTemplateChildren())
  218. {
  219. child.SetValue(TemplatedParentProperty, null);
  220. }
  221. VisualChildren.Clear();
  222. }
  223. if (template != null)
  224. {
  225. Logger.Verbose(LogArea.Control, this, "Creating control template");
  226. var child = template.Build(this);
  227. var nameScope = new NameScope();
  228. NameScope.SetNameScope((Control)child, nameScope);
  229. child.SetValue(TemplatedParentProperty, this);
  230. RegisterNames(child, nameScope);
  231. ((ISetLogicalParent)child).SetParent(this);
  232. VisualChildren.Add(child);
  233. OnTemplateApplied(new TemplateAppliedEventArgs(nameScope));
  234. }
  235. _appliedTemplate = template;
  236. }
  237. }
  238. /// <inheritdoc/>
  239. protected override IControl GetTemplateFocusTarget()
  240. {
  241. foreach (Control child in this.GetTemplateChildren())
  242. {
  243. if (GetIsTemplateFocusTarget(child))
  244. {
  245. return child;
  246. }
  247. }
  248. return this;
  249. }
  250. /// <inheritdoc/>
  251. protected override void OnAttachedToLogicalTree(LogicalTreeAttachmentEventArgs e)
  252. {
  253. if (VisualChildren.Count > 0)
  254. {
  255. ((ILogical)VisualChildren[0]).NotifyAttachedToLogicalTree(e);
  256. }
  257. base.OnAttachedToLogicalTree(e);
  258. }
  259. /// <inheritdoc/>
  260. protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
  261. {
  262. if (VisualChildren.Count > 0)
  263. {
  264. ((ILogical)VisualChildren[0]).NotifyDetachedFromLogicalTree(e);
  265. }
  266. base.OnDetachedFromLogicalTree(e);
  267. }
  268. /// <summary>
  269. /// Called when the control's template is applied.
  270. /// </summary>
  271. /// <param name="e">The event args.</param>
  272. protected virtual void OnTemplateApplied(TemplateAppliedEventArgs e)
  273. {
  274. RaiseEvent(e);
  275. }
  276. /// <summary>
  277. /// Called when the <see cref="Template"/> property changes.
  278. /// </summary>
  279. /// <param name="e">The event args.</param>
  280. protected virtual void OnTemplateChanged(AvaloniaPropertyChangedEventArgs e)
  281. {
  282. InvalidateMeasure();
  283. }
  284. /// <summary>
  285. /// Registers each control with its name scope.
  286. /// </summary>
  287. /// <param name="control">The control.</param>
  288. /// <param name="nameScope">The name scope.</param>
  289. private void RegisterNames(IControl control, INameScope nameScope)
  290. {
  291. if (control.Name != null)
  292. {
  293. nameScope.Register(control.Name, control);
  294. }
  295. if (control.TemplatedParent == this)
  296. {
  297. foreach (IControl child in control.GetVisualChildren())
  298. {
  299. RegisterNames(child, nameScope);
  300. }
  301. }
  302. }
  303. }
  304. }