Browse Source

Added DataTemplates.

pull/4/head
Steven Kirk 12 years ago
parent
commit
1ee1e0b85d
  1. 1
      Perspex.Direct2D1.RenderTests/Controls/ImageTests.cs
  2. 58
      Perspex/Controls/ContentPresenter.cs
  3. 20
      Perspex/Controls/Control.cs
  4. 42
      Perspex/Controls/DataTemplate.cs
  5. 20
      Perspex/Controls/DataTemplates.cs
  6. 3
      Perspex/Perspex.csproj
  7. 38
      Perspex/Themes/Default/ContentControlStyle.cs
  8. 2
      Perspex/Themes/Default/DefaultTheme.cs
  9. 19
      TestApplication/Program.cs

1
Perspex.Direct2D1.RenderTests/Controls/ImageTests.cs

@ -9,7 +9,6 @@ namespace Perspex.Direct2D1.RenderTests.Controls
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Perspex.Controls;
using Perspex.Layout;
using Perspex.Media;
using Perspex.Media.Imaging;

58
Perspex/Controls/ContentPresenter.cs

@ -20,7 +20,7 @@ namespace Perspex.Controls
public static readonly PerspexProperty<Func<object, Visual>> DataTemplateProperty =
PerspexProperty.Register<ContentPresenter, Func<object, Visual>>("DataTemplate");
private Visual visualChild;
private IVisual visualChild;
public ContentPresenter()
{
@ -46,18 +46,11 @@ namespace Perspex.Controls
set { this.SetValue(ContentProperty, value); }
}
public Func<object, Visual> DataTemplate
{
get { return this.GetValue(DataTemplateProperty); }
set { this.SetValue(DataTemplateProperty, value); }
}
IEnumerable<IVisual> IVisual.VisualChildren
{
get
{
object content = this.Content;
var dataTemplate = this.DataTemplate;
if (this.visualChild == null && content != null)
{
@ -65,16 +58,21 @@ namespace Perspex.Controls
{
this.visualChild = (Visual)content;
}
else if (dataTemplate != null)
{
this.visualChild = dataTemplate(this);
}
else
{
this.visualChild = new TextBlock
DataTemplate dataTemplate = this.FindDataTemplate(content);
if (dataTemplate != null)
{
Text = content.ToString(),
};
this.visualChild = dataTemplate.Build(content);
}
else
{
this.visualChild = new TextBlock
{
Text = content.ToString(),
};
}
}
if (this.visualChild != null)
@ -156,5 +154,35 @@ namespace Perspex.Controls
return new Size();
}
private DataTemplate FindDataTemplate(object content)
{
ILogical node = this;
while (node != null)
{
Control control = node as Control;
if (control != null)
{
foreach (DataTemplate dt in control.DataTemplates.Reverse())
{
if (dt.Match(content))
{
return dt;
}
}
}
node = node.LogicalParent;
if (node == null && control != null)
{
node = control.TemplatedParent as ILogical;
}
}
return null;
}
}
}

20
Perspex/Controls/Control.cs

@ -40,6 +40,8 @@ namespace Perspex.Controls
private Classes classes;
private DataTemplates dataTemplates;
private string id;
private Styles styles;
@ -91,6 +93,24 @@ namespace Perspex.Controls
}
}
public DataTemplates DataTemplates
{
get
{
if (this.dataTemplates == null)
{
this.dataTemplates = new DataTemplates();
}
return this.dataTemplates;
}
set
{
this.dataTemplates = value;
}
}
public double FontSize
{
get { return this.GetValue(FontSizeProperty); }

42
Perspex/Controls/DataTemplate.cs

@ -0,0 +1,42 @@
// -----------------------------------------------------------------------
// <copyright file="DataTemplate.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
using System;
using System.Reflection;
public class DataTemplate
{
public DataTemplate(Type type, Func<object, IVisual> build)
: this(o => type.GetTypeInfo().IsAssignableFrom(o.GetType().GetTypeInfo()), build)
{
}
public DataTemplate(Func<object, bool> match, Func<object, IVisual> build)
{
this.Match = match;
this.Build = build;
}
public Func<object, bool> Match { get; private set; }
public Func<object, IVisual> Build { get; private set; }
}
public class DataTemplate<T> : DataTemplate
{
public DataTemplate(Func<T, IVisual> build)
: base(typeof(T), o => build((T)o))
{
}
public DataTemplate(Func<T, bool> match, Func<T, IVisual> build)
: base(o => (o is T) ? match((T)o) : false, o => build((T)o))
{
}
}
}

20
Perspex/Controls/DataTemplates.cs

@ -0,0 +1,20 @@
// -----------------------------------------------------------------------
// <copyright file="DataTemplates.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
using System.Reactive;
using System.Reactive.Subjects;
public class DataTemplates : PerspexList<DataTemplate>
{
}
}

3
Perspex/Perspex.csproj

@ -70,6 +70,8 @@
<ItemGroup>
<Compile Include="Application.cs" />
<Compile Include="BindingExtensions.cs" />
<Compile Include="Controls\DataTemplate.cs" />
<Compile Include="Controls\DataTemplates.cs" />
<Compile Include="Controls\CheckBox.cs" />
<Compile Include="Controls\ColumnDefinition.cs" />
<Compile Include="Controls\ColumnDefinitions.cs" />
@ -121,6 +123,7 @@
<Compile Include="Media\StreamGeometry.cs" />
<Compile Include="Media\Geometry.cs" />
<Compile Include="Media\Stretch.cs" />
<Compile Include="Themes\Default\ContentControlStyle.cs" />
<Compile Include="Vector.cs" />
<Compile Include="Rendering\RenderManager.cs" />
<Compile Include="Rendering\IRenderManager.cs" />

38
Perspex/Themes/Default/ContentControlStyle.cs

@ -0,0 +1,38 @@
// -----------------------------------------------------------------------
// <copyright file="ContentControlStyle.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Themes.Default
{
using System.Linq;
using Perspex.Controls;
using Perspex.Media;
using Perspex.Styling;
public class ContentControlStyle : Styles
{
public ContentControlStyle()
{
this.AddRange(new[]
{
new Style(x => x.OfType<ContentControl>())
{
Setters = new[]
{
new Setter(Button.TemplateProperty, ControlTemplate.Create<ContentControl>(this.Template)),
},
},
});
}
private Control Template(ContentControl control)
{
return new ContentPresenter
{
[~ContentPresenter.ContentProperty] = control[~ContentControl.ContentProperty],
};
}
}
}

2
Perspex/Themes/Default/DefaultTheme.cs

@ -14,8 +14,8 @@ namespace Perspex.Themes.Default
{
this.Add(new ButtonStyle());
this.Add(new CheckBoxStyle());
this.Add(new ContentControlStyle());
this.Add(new TextBoxStyle());
}
}
}

19
TestApplication/Program.cs

@ -75,7 +75,24 @@ namespace TestApplication
{
Source = new Bitmap("github_icon.png"),
Width = 200,
}
},
new ContentControl
{
DataTemplates = new DataTemplates
{
new DataTemplate<string>(o => new Border
{
Background = Brushes.Red,
BorderBrush = Brushes.Black,
BorderThickness = 2,
Content = new TextBlock
{
Text = o,
}
}),
},
Content = "Data Template",
},
}
}
};

Loading…
Cancel
Save