8 changed files with 247 additions and 28 deletions
@ -1,25 +1,55 @@ |
|||||
namespace Perspex |
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="ControlTemplate.cs" company="Steven Kirk">
|
||||
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex |
||||
{ |
{ |
||||
using System; |
using System; |
||||
|
using System.Diagnostics.Contracts; |
||||
|
using System.Linq; |
||||
using Perspex.Controls; |
using Perspex.Controls; |
||||
|
|
||||
public class ControlTemplate |
public class ControlTemplate |
||||
{ |
{ |
||||
|
private Func<TemplatedControl, Control> build; |
||||
|
|
||||
public ControlTemplate(Func<TemplatedControl, Control> build) |
public ControlTemplate(Func<TemplatedControl, Control> build) |
||||
{ |
{ |
||||
this.Build = build; |
Contract.Requires<NullReferenceException>(build != null); |
||||
|
|
||||
|
this.build = build; |
||||
} |
} |
||||
|
|
||||
public Func<TemplatedControl, Control> Build |
public Control Build(TemplatedControl templatedParent) |
||||
{ |
{ |
||||
get; |
Contract.Requires<NullReferenceException>(templatedParent != null); |
||||
private set; |
|
||||
|
Control root = this.build(templatedParent); |
||||
|
this.SetTemplatedParent(root, templatedParent); |
||||
|
return root; |
||||
} |
} |
||||
|
|
||||
public static ControlTemplate Create<TControl>(Func<TControl, Control> build) |
public static ControlTemplate Create<TControl>(Func<TControl, Control> build) |
||||
where TControl : TemplatedControl |
where TControl : TemplatedControl |
||||
{ |
{ |
||||
|
Contract.Requires<NullReferenceException>(build != null); |
||||
|
|
||||
return new ControlTemplate(c => build((TControl)c)); |
return new ControlTemplate(c => build((TControl)c)); |
||||
} |
} |
||||
|
|
||||
|
private void SetTemplatedParent(Control control, TemplatedControl templatedParent) |
||||
|
{ |
||||
|
Contract.Requires<NullReferenceException>(control != null); |
||||
|
Contract.Requires<NullReferenceException>(templatedParent != null); |
||||
|
|
||||
|
control.TemplatedParent = templatedParent; |
||||
|
|
||||
|
foreach (Control child in control.VisualChildren.OfType<Control>()) |
||||
|
{ |
||||
|
this.SetTemplatedParent(child, templatedParent); |
||||
|
} |
||||
|
} |
||||
} |
} |
||||
} |
} |
||||
|
|||||
Loading…
Reference in new issue