From c0fc38358f80c585b14aaad9664495dc6197c4f3 Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Mon, 27 Jul 2015 20:19:08 +0200 Subject: [PATCH] Refactored templates a bit. Added interfaces, moved them to Controls.Templates namespace and added base classes for common Func<> templates. --- Perspex.Application/Application.cs | 1 + Perspex.Controls/Control.cs | 1 + Perspex.Controls/ControlTemplate.cs | 39 --------- Perspex.Controls/DataTemplate.cs | 80 ------------------ .../Generators/ItemContainerGenerator.cs | 2 +- Perspex.Controls/IControl.cs | 1 + Perspex.Controls/IDataTemplate.cs | 15 ---- Perspex.Controls/IGlobalDataTemplates.cs | 1 + Perspex.Controls/Perspex.Controls.csproj | 15 +++- .../Presenters/ContentPresenter.cs | 2 +- Perspex.Controls/Templates/ControlTemplate.cs | 27 ++++++ .../Templates/ControlTemplate`2.cs | 28 +++++++ Perspex.Controls/Templates/DataTemplate.cs | 84 +++++++++++++++++++ .../Templates/DataTemplateExtensions.cs | 4 +- Perspex.Controls/Templates/DataTemplate`1.cs | 63 ++++++++++++++ .../{ => Templates}/DataTemplates.cs | 9 +- Perspex.Controls/Templates/FuncTemplate`1.cs | 41 +++++++++ Perspex.Controls/Templates/FuncTemplate`2.cs | 44 ++++++++++ .../Templates/IControlTemplate.cs | 18 ++++ Perspex.Controls/Templates/IDataTemplate.cs | 23 +++++ Perspex.Controls/Templates/ITemplate`1.cs | 23 +++++ Perspex.Controls/Templates/ITemplate`2.cs | 25 ++++++ Perspex.Controls/TreeDataTemplate.cs | 31 +++++-- Perspex.Diagnostics/ViewLocator.cs | 3 +- Perspex.Diagnostics/Views/LogicalTreeView.cs | 7 +- Perspex.Diagnostics/Views/VisualTreeView.cs | 1 + Perspex.Themes.Default/ButtonStyle.cs | 3 +- Perspex.Themes.Default/CheckBoxStyle.cs | 8 +- Perspex.Themes.Default/ContentControlStyle.cs | 4 +- Perspex.Themes.Default/DeckStyle.cs | 5 +- Perspex.Themes.Default/DropDownStyle.cs | 4 +- Perspex.Themes.Default/GridSplitterStyle.cs | 5 +- Perspex.Themes.Default/ItemsControlStyle.cs | 3 +- Perspex.Themes.Default/ListBoxItemStyle.cs | 5 +- Perspex.Themes.Default/ListBoxStyle.cs | 5 +- Perspex.Themes.Default/MenuItemStyle.cs | 5 +- Perspex.Themes.Default/MenuStyle.cs | 3 +- Perspex.Themes.Default/PopupRootStyle.cs | 4 +- Perspex.Themes.Default/ProgressBarStyle.cs | 3 +- Perspex.Themes.Default/RadioButtonStyle.cs | 8 +- Perspex.Themes.Default/ScrollBarStyle.cs | 9 +- Perspex.Themes.Default/ScrollViewerStyle.cs | 4 +- Perspex.Themes.Default/TabControlStyle.cs | 9 +- Perspex.Themes.Default/TabItemStyle.cs | 3 +- Perspex.Themes.Default/TabStripStyle.cs | 3 +- Perspex.Themes.Default/TextBoxStyle.cs | 7 +- Perspex.Themes.Default/ToggleButtonStyle.cs | 3 +- Perspex.Themes.Default/ToolTipStyle.cs | 5 +- Perspex.Themes.Default/TreeViewItemStyle.cs | 11 +-- Perspex.Themes.Default/TreeViewStyle.cs | 3 +- Perspex.Themes.Default/WindowStyle.cs | 4 +- TestApplication/Program.cs | 4 +- .../ContentControlTests.cs | 2 +- Tests/Perspex.Controls.UnitTests/DeckTests.cs | 7 +- .../DropDownTests.cs | 2 +- .../ItemsControlTests.cs | 2 +- .../ListBoxTests.cs | 1 + .../Primitives/ScrollBarTests.cs | 8 +- .../Primitives/SelectingItemsControlTests.cs | 3 +- .../ScrollViewerTests.cs | 6 +- .../TabControlTests.cs | 16 ++-- .../TabStripTests.cs | 9 +- .../TopLevelTests.cs | 9 +- .../SelectorTests_Multiple.cs | 1 + 64 files changed, 537 insertions(+), 247 deletions(-) delete mode 100644 Perspex.Controls/ControlTemplate.cs delete mode 100644 Perspex.Controls/DataTemplate.cs delete mode 100644 Perspex.Controls/IDataTemplate.cs create mode 100644 Perspex.Controls/Templates/ControlTemplate.cs create mode 100644 Perspex.Controls/Templates/ControlTemplate`2.cs create mode 100644 Perspex.Controls/Templates/DataTemplate.cs create mode 100644 Perspex.Controls/Templates/DataTemplate`1.cs rename Perspex.Controls/{ => Templates}/DataTemplates.cs (63%) create mode 100644 Perspex.Controls/Templates/FuncTemplate`1.cs create mode 100644 Perspex.Controls/Templates/FuncTemplate`2.cs create mode 100644 Perspex.Controls/Templates/IControlTemplate.cs create mode 100644 Perspex.Controls/Templates/IDataTemplate.cs create mode 100644 Perspex.Controls/Templates/ITemplate`1.cs create mode 100644 Perspex.Controls/Templates/ITemplate`2.cs diff --git a/Perspex.Application/Application.cs b/Perspex.Application/Application.cs index 4bd43a5984..173e4f2504 100644 --- a/Perspex.Application/Application.cs +++ b/Perspex.Application/Application.cs @@ -9,6 +9,7 @@ namespace Perspex using System; using System.Threading; using Perspex.Controls; + using Perspex.Controls.Templates; using Perspex.Input; using Perspex.Layout; using Perspex.Rendering; diff --git a/Perspex.Controls/Control.cs b/Perspex.Controls/Control.cs index ab7ccab3e3..3b3e641062 100644 --- a/Perspex.Controls/Control.cs +++ b/Perspex.Controls/Control.cs @@ -11,6 +11,7 @@ namespace Perspex.Controls using System.Reactive.Linq; using Perspex.Collections; using Perspex.Controls.Primitives; + using Perspex.Controls.Templates; using Perspex.Input; using Perspex.Interactivity; using Perspex.LogicalTree; diff --git a/Perspex.Controls/ControlTemplate.cs b/Perspex.Controls/ControlTemplate.cs deleted file mode 100644 index 62852c58bd..0000000000 --- a/Perspex.Controls/ControlTemplate.cs +++ /dev/null @@ -1,39 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.Controls -{ - using System; - using Perspex.Styling; - - public class ControlTemplate - { - private Func build; - - public ControlTemplate(Func build) - { - Contract.Requires(build != null); - - this.build = build; - } - - public static ControlTemplate Create(Func build) - where TControl : ITemplatedControl - { - Contract.Requires(build != null); - - return new ControlTemplate(c => build((TControl)c)); - } - - public Control Build(ITemplatedControl templatedParent) - { - Contract.Requires(templatedParent != null); - - var root = this.build(templatedParent); - return root; - } - } -} diff --git a/Perspex.Controls/DataTemplate.cs b/Perspex.Controls/DataTemplate.cs deleted file mode 100644 index 85d2d12a46..0000000000 --- a/Perspex.Controls/DataTemplate.cs +++ /dev/null @@ -1,80 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.Controls -{ - using System; - using System.Reflection; - - public class DataTemplate : IDataTemplate - { - public static readonly DataTemplate Default = - new DataTemplate(typeof(object), o => (o != null) ? new TextBlock { Text = o.ToString() } : null); - - public DataTemplate(Func build) - : this(o => true, build) - { - } - - public DataTemplate(Type type, Func build) - : this(o => IsInstance(o, type), build) - { - } - - public DataTemplate(Func match, Func build) - { - Contract.Requires(match != null); - Contract.Requires(build != null); - - this.Match = match; - this.Build = build; - } - - public Func Match { get; private set; } - - public Func Build { get; private set; } - - public static bool IsInstance(object o, Type t) - { - return (o != null) ? - t.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()) : - false; - } - - bool IDataTemplate.Match(object data) - { - return this.Match(data); - } - - Control IDataTemplate.Build(object data) - { - return this.Build(data); - } - } - - public class DataTemplate : DataTemplate - { - public DataTemplate(Func build) - : base(typeof(T), DataTemplate.Cast(build)) - { - } - - public DataTemplate(Func match, Func build) - : base(CastMatch(match), DataTemplate.Cast(build)) - { - } - - private static Func CastMatch(Func f) - { - return o => (o is T) ? f((T)o) : false; - } - - private static Func Cast(Func f) - { - return o => f((T)o); - } - } -} diff --git a/Perspex.Controls/Generators/ItemContainerGenerator.cs b/Perspex.Controls/Generators/ItemContainerGenerator.cs index eff9ca06f5..1bd0d5a812 100644 --- a/Perspex.Controls/Generators/ItemContainerGenerator.cs +++ b/Perspex.Controls/Generators/ItemContainerGenerator.cs @@ -126,7 +126,7 @@ namespace Perspex.Controls.Generators protected virtual Control CreateContainerOverride(object item) { - return this.Owner.MaterializeDataTemplate(item); + return (Control)this.Owner.MaterializeDataTemplate(item); } protected void AddInternal(object item, Control container) diff --git a/Perspex.Controls/IControl.cs b/Perspex.Controls/IControl.cs index ee893658c6..f0b3a5236f 100644 --- a/Perspex.Controls/IControl.cs +++ b/Perspex.Controls/IControl.cs @@ -6,6 +6,7 @@ namespace Perspex.Controls { + using Perspex.Controls.Templates; using Perspex.Input; using Perspex.Layout; using Perspex.Styling; diff --git a/Perspex.Controls/IDataTemplate.cs b/Perspex.Controls/IDataTemplate.cs deleted file mode 100644 index 30d47d6bc5..0000000000 --- a/Perspex.Controls/IDataTemplate.cs +++ /dev/null @@ -1,15 +0,0 @@ -// ----------------------------------------------------------------------- -// -// Copyright 2014 MIT Licence. See licence.md for more information. -// -// ----------------------------------------------------------------------- - -namespace Perspex.Controls -{ - public interface IDataTemplate - { - bool Match(object data); - - Control Build(object data); - } -} diff --git a/Perspex.Controls/IGlobalDataTemplates.cs b/Perspex.Controls/IGlobalDataTemplates.cs index 71e469e106..a112bc019e 100644 --- a/Perspex.Controls/IGlobalDataTemplates.cs +++ b/Perspex.Controls/IGlobalDataTemplates.cs @@ -7,6 +7,7 @@ namespace Perspex.Controls { using System.Collections.Generic; + using Perspex.Controls.Templates; public interface IGlobalDataTemplates { diff --git a/Perspex.Controls/Perspex.Controls.csproj b/Perspex.Controls/Perspex.Controls.csproj index 8ee82358c7..db1499a57d 100644 --- a/Perspex.Controls/Perspex.Controls.csproj +++ b/Perspex.Controls/Perspex.Controls.csproj @@ -58,7 +58,12 @@ - + + + + + + @@ -81,14 +86,14 @@ - + - + - + @@ -107,6 +112,8 @@ + + diff --git a/Perspex.Controls/Presenters/ContentPresenter.cs b/Perspex.Controls/Presenters/ContentPresenter.cs index aaf110c0eb..64c56082ac 100644 --- a/Perspex.Controls/Presenters/ContentPresenter.cs +++ b/Perspex.Controls/Presenters/ContentPresenter.cs @@ -86,7 +86,7 @@ namespace Perspex.Controls.Presenters if (content != null) { - result = this.MaterializeDataTemplate(content); + result = (Control)this.MaterializeDataTemplate(content); if (result.Parent == null) { diff --git a/Perspex.Controls/Templates/ControlTemplate.cs b/Perspex.Controls/Templates/ControlTemplate.cs new file mode 100644 index 0000000000..50dc546693 --- /dev/null +++ b/Perspex.Controls/Templates/ControlTemplate.cs @@ -0,0 +1,27 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates +{ + using System; + using Perspex.Controls.Primitives; + using Perspex.Styling; + + /// + /// A template for a . + /// + public class ControlTemplate : FuncTemplate, IControlTemplate + { + /// + /// Initializes a new instance of the class. + /// + /// The build function. + public ControlTemplate(Func build) + : base(build) + { + } + } +} \ No newline at end of file diff --git a/Perspex.Controls/Templates/ControlTemplate`2.cs b/Perspex.Controls/Templates/ControlTemplate`2.cs new file mode 100644 index 0000000000..870cb94673 --- /dev/null +++ b/Perspex.Controls/Templates/ControlTemplate`2.cs @@ -0,0 +1,28 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates +{ + using System; + using Perspex.Controls.Primitives; + using Styling; + + /// + /// A template for a . + /// + /// The type of the lookless control. + public class ControlTemplate : ControlTemplate where T : ITemplatedControl + { + /// + /// Initializes a new instance of the class. + /// + /// The build function. + public ControlTemplate(Func build) + : base(x => build((T)x)) + { + } + } +} \ No newline at end of file diff --git a/Perspex.Controls/Templates/DataTemplate.cs b/Perspex.Controls/Templates/DataTemplate.cs new file mode 100644 index 0000000000..742cd5cdf0 --- /dev/null +++ b/Perspex.Controls/Templates/DataTemplate.cs @@ -0,0 +1,84 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates +{ + using System; + using System.Reflection; + + /// + /// Builds a control for a piece of data. + /// + public class DataTemplate : FuncTemplate, IDataTemplate + { + /// + /// The default data template used in the case where not matching data template is found. + /// + public static readonly DataTemplate Default = + new DataTemplate(typeof(object), o => (o != null) ? new TextBlock { Text = o.ToString() } : null); + + /// + /// The implementation of the method. + /// + private Func match; + + /// + /// Initializes a new instance of the class. + /// + /// The type of data which the data template matches. + /// + /// A function which when passed an object of returns a control. + /// + public DataTemplate(Type type, Func build) + : this(o => IsInstance(o, type), build) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// A function which determines whether the data template matches the specified data. + /// + /// + /// A function which returns a control for matching data. + /// + public DataTemplate(Func match, Func build) + : base(build) + { + Contract.Requires(match != null); + + this.match = match; + } + + /// + /// Checks to see if this data template matches the specified data. + /// + /// The data. + /// + /// True if the data template can build a control for the data, otherwise false. + /// + public bool Match(object data) + { + return this.match(data); + } + + /// + /// Determines of an object is of the specified type. + /// + /// The object. + /// The type. + /// + /// True if is of type , otherwise false. + /// + private static bool IsInstance(object o, Type t) + { + return (o != null) ? + t.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()) : + false; + } + } +} \ No newline at end of file diff --git a/Perspex.Controls/Templates/DataTemplateExtensions.cs b/Perspex.Controls/Templates/DataTemplateExtensions.cs index f9b326c366..5f352ec3a9 100644 --- a/Perspex.Controls/Templates/DataTemplateExtensions.cs +++ b/Perspex.Controls/Templates/DataTemplateExtensions.cs @@ -12,10 +12,10 @@ namespace Perspex.Controls.Templates public static class DataTemplateExtensions { - public static Control MaterializeDataTemplate(this Control control, object data) + public static IControl MaterializeDataTemplate(this Control control, object data) { IDataTemplate template = control.FindDataTemplate(data); - Control result; + IControl result; if (template != null) { diff --git a/Perspex.Controls/Templates/DataTemplate`1.cs b/Perspex.Controls/Templates/DataTemplate`1.cs new file mode 100644 index 0000000000..d3b0acc7f1 --- /dev/null +++ b/Perspex.Controls/Templates/DataTemplate`1.cs @@ -0,0 +1,63 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates +{ + using System; + + /// + /// Builds a control for a piece of data of specified type. + /// + /// The type of the template's data. + public class DataTemplate : DataTemplate + { + /// + /// Initializes a new instance of the class. + /// + /// + /// A function which when passed an object of returns a control. + /// + public DataTemplate(Func build) + : base(typeof(T), CastBuild(build)) + { + } + + /// + /// Initializes a new instance of the class. + /// + /// + /// A function which determines whether the data template matches the specified data. + /// + /// + /// A function which when passed an object of returns a control. + /// + public DataTemplate(Func match, Func build) + : base(CastMatch(match), CastBuild(build)) + { + } + + /// + /// Casts a stongly typed match function to a weakly typed one. + /// + /// The strongly typed function. + /// The weakly typed function. + private static Func CastMatch(Func f) + { + return o => (o is T) ? f((T)o) : false; + } + + /// + /// Casts a stongly typed build function to a weakly typed one. + /// + /// The strong data type. + /// The strongly typed function. + /// The weakly typed function. + private static Func CastBuild(Func f) + { + return o => f((T)o); + } + } +} \ No newline at end of file diff --git a/Perspex.Controls/DataTemplates.cs b/Perspex.Controls/Templates/DataTemplates.cs similarity index 63% rename from Perspex.Controls/DataTemplates.cs rename to Perspex.Controls/Templates/DataTemplates.cs index c06b5bbf6f..fbd4e3789a 100644 --- a/Perspex.Controls/DataTemplates.cs +++ b/Perspex.Controls/Templates/DataTemplates.cs @@ -1,14 +1,17 @@ // ----------------------------------------------------------------------- // -// Copyright 2014 MIT Licence. See licence.md for more information. +// Copyright 2015 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- -namespace Perspex.Controls +namespace Perspex.Controls.Templates { using Perspex.Collections; + /// + /// A collection of s. + /// public class DataTemplates : PerspexList { } -} +} \ No newline at end of file diff --git a/Perspex.Controls/Templates/FuncTemplate`1.cs b/Perspex.Controls/Templates/FuncTemplate`1.cs new file mode 100644 index 0000000000..489cf8cbe3 --- /dev/null +++ b/Perspex.Controls/Templates/FuncTemplate`1.cs @@ -0,0 +1,41 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates +{ + using System; + + /// + /// Creates a control from a . + /// + /// The type of control. + public class FuncTemplate : ITemplate where TControl : IControl + { + private Func func; + + /// + /// Initializes a new instance of the class. + /// + /// The function used to create the control. + public FuncTemplate(Func func) + { + Contract.Requires(func != null); + + this.func = func; + } + + /// + /// Creates the control. + /// + /// + /// The created control. + /// + public TControl Build() + { + return this.func(); + } + } +} \ No newline at end of file diff --git a/Perspex.Controls/Templates/FuncTemplate`2.cs b/Perspex.Controls/Templates/FuncTemplate`2.cs new file mode 100644 index 0000000000..70b0c4f73d --- /dev/null +++ b/Perspex.Controls/Templates/FuncTemplate`2.cs @@ -0,0 +1,44 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates +{ + using System; + + /// + /// Creates a control from a . + /// + /// The type of the parameter. + /// The type of control. + public class FuncTemplate : ITemplate + where TControl : IControl + { + private Func func; + + /// + /// Initializes a new instance of the class. + /// + /// The function used to create the control. + public FuncTemplate(Func func) + { + Contract.Requires(func != null); + + this.func = func; + } + + /// + /// Creates the control. + /// + /// The parameter. + /// + /// The created control. + /// + public TControl Build(TParam param) + { + return this.func(param); + } + } +} \ No newline at end of file diff --git a/Perspex.Controls/Templates/IControlTemplate.cs b/Perspex.Controls/Templates/IControlTemplate.cs new file mode 100644 index 0000000000..bdd6542efe --- /dev/null +++ b/Perspex.Controls/Templates/IControlTemplate.cs @@ -0,0 +1,18 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates +{ + using Perspex.Controls.Primitives; + using Perspex.Styling; + + /// + /// Interface representing a template used to build a . + /// + public interface IControlTemplate : ITemplate + { + } +} \ No newline at end of file diff --git a/Perspex.Controls/Templates/IDataTemplate.cs b/Perspex.Controls/Templates/IDataTemplate.cs new file mode 100644 index 0000000000..f43578655c --- /dev/null +++ b/Perspex.Controls/Templates/IDataTemplate.cs @@ -0,0 +1,23 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates +{ + /// + /// Interface representing a template used to build a control for a piece of data. + /// + public interface IDataTemplate : ITemplate + { + /// + /// Checks to see if this data template matches the specified data. + /// + /// The data. + /// + /// True if the data template can build a control for the data, otherwise false. + /// + bool Match(object data); + } +} \ No newline at end of file diff --git a/Perspex.Controls/Templates/ITemplate`1.cs b/Perspex.Controls/Templates/ITemplate`1.cs new file mode 100644 index 0000000000..ca78699b5f --- /dev/null +++ b/Perspex.Controls/Templates/ITemplate`1.cs @@ -0,0 +1,23 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls +{ + /// + /// Creates a control. + /// + /// The type of control. + public interface ITemplate where TControl : IControl + { + /// + /// Creates the control. + /// + /// + /// The created control. + /// + TControl Build(); + } +} \ No newline at end of file diff --git a/Perspex.Controls/Templates/ITemplate`2.cs b/Perspex.Controls/Templates/ITemplate`2.cs new file mode 100644 index 0000000000..0b95cea31c --- /dev/null +++ b/Perspex.Controls/Templates/ITemplate`2.cs @@ -0,0 +1,25 @@ +// ----------------------------------------------------------------------- +// +// Copyright 2015 MIT Licence. See licence.md for more information. +// +// ----------------------------------------------------------------------- + +namespace Perspex.Controls.Templates +{ + /// + /// Creates a control based on a parameter. + /// + /// The type of the parameter. + /// The type of control. + public interface ITemplate where TControl : IControl + { + /// + /// Creates the control. + /// + /// The parameter. + /// + /// The created control. + /// + TControl Build(TParam param); + } +} \ No newline at end of file diff --git a/Perspex.Controls/TreeDataTemplate.cs b/Perspex.Controls/TreeDataTemplate.cs index b72af87c2b..eec4bcf5af 100644 --- a/Perspex.Controls/TreeDataTemplate.cs +++ b/Perspex.Controls/TreeDataTemplate.cs @@ -8,11 +8,13 @@ namespace Perspex.Controls { using System; using System.Collections; + using System.Reflection; + using Perspex.Controls.Templates; public class TreeDataTemplate : DataTemplate { public TreeDataTemplate( - Func build, + Func build, Func itemsSelector) : this(o => true, build, itemsSelector) { @@ -20,24 +22,24 @@ namespace Perspex.Controls public TreeDataTemplate( Type type, - Func build, + Func build, Func itemsSelector) - : this(o => DataTemplate.IsInstance(o, type), build, itemsSelector) + : this(o => IsInstance(o, type), build, itemsSelector) { } public TreeDataTemplate( Type type, - Func build, + Func build, Func itemsSelector, Func isExpanded) - : this(o => DataTemplate.IsInstance(o, type), build, itemsSelector, isExpanded) + : this(o => IsInstance(o, type), build, itemsSelector, isExpanded) { } public TreeDataTemplate( Func match, - Func build, + Func build, Func itemsSelector) : this(match, build, itemsSelector, _ => false) { @@ -46,7 +48,7 @@ namespace Perspex.Controls public TreeDataTemplate( Func match, - Func build, + Func build, Func itemsSelector, Func isExpanded) : base(match, build) @@ -58,6 +60,21 @@ namespace Perspex.Controls public Func ItemsSelector { get; private set; } public Func IsExpanded { get; private set; } + + /// + /// Determines of an object is of the specified type. + /// + /// The object. + /// The type. + /// + /// True if is of type , otherwise false. + /// + private static bool IsInstance(object o, Type t) + { + return (o != null) ? + t.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()) : + false; + } } public class TreeDataTemplate : TreeDataTemplate diff --git a/Perspex.Diagnostics/ViewLocator.cs b/Perspex.Diagnostics/ViewLocator.cs index 7da3ba0489..f2f16796c3 100644 --- a/Perspex.Diagnostics/ViewLocator.cs +++ b/Perspex.Diagnostics/ViewLocator.cs @@ -8,10 +8,11 @@ namespace Perspex.Diagnostics { using System; using Perspex.Controls; + using Perspex.Controls.Templates; internal class ViewLocator : IDataTemplate { - public Control Build(object data) + public IControl Build(object data) { var name = data.GetType().FullName.Replace("ViewModel", "View"); var type = Type.GetType(name); diff --git a/Perspex.Diagnostics/Views/LogicalTreeView.cs b/Perspex.Diagnostics/Views/LogicalTreeView.cs index 53f1435023..3d464d3a70 100644 --- a/Perspex.Diagnostics/Views/LogicalTreeView.cs +++ b/Perspex.Diagnostics/Views/LogicalTreeView.cs @@ -1,16 +1,17 @@ // ----------------------------------------------------------------------- -// +// // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Diagnostics.Views { + using System; + using System.Reactive.Linq; using Perspex.Controls; + using Perspex.Controls.Templates; using Perspex.Diagnostics.ViewModels; using ReactiveUI; - using System; - using System.Reactive.Linq; internal class LogicalTreeView : TreePage { diff --git a/Perspex.Diagnostics/Views/VisualTreeView.cs b/Perspex.Diagnostics/Views/VisualTreeView.cs index a1130fb957..4dc7313184 100644 --- a/Perspex.Diagnostics/Views/VisualTreeView.cs +++ b/Perspex.Diagnostics/Views/VisualTreeView.cs @@ -9,6 +9,7 @@ namespace Perspex.Diagnostics.Views using System; using System.Reactive.Linq; using Perspex.Controls; + using Perspex.Controls.Templates; using Perspex.Diagnostics.ViewModels; using Perspex.Media; using ReactiveUI; diff --git a/Perspex.Themes.Default/ButtonStyle.cs b/Perspex.Themes.Default/ButtonStyle.cs index 2fcc277a25..cd36396653 100644 --- a/Perspex.Themes.Default/ButtonStyle.cs +++ b/Perspex.Themes.Default/ButtonStyle.cs @@ -11,6 +11,7 @@ namespace Perspex.Themes.Default using Perspex.Controls; using Perspex.Controls.Presenters; using Perspex.Controls.Shapes; + using Perspex.Controls.Templates; using Perspex.Layout; using Perspex.Media; using Perspex.Styling; @@ -26,7 +27,7 @@ namespace Perspex.Themes.Default Setters = new[] { new Setter(Button.FocusAdornerProperty, new AdornerTemplate(FocusAdornerTemplate)), - new Setter(Button.TemplateProperty, ControlTemplate.Create