Browse Source

More work on TreeView.

pull/4/head
Steven Kirk 12 years ago
parent
commit
0cf5cf6abc
  1. 4
      Perspex/Controls/Control.cs
  2. 14
      Perspex/Controls/DataTemplate.cs
  3. 53
      Perspex/Controls/TreeDataTemplate.cs
  4. 1
      Perspex/Controls/TreeView.cs
  5. 20
      Perspex/Controls/TreeViewItem.cs
  6. 22
      Perspex/Visual.cs
  7. 10
      Perspex/VisualExtensions.cs
  8. 3
      TestApplication/Program.cs

4
Perspex/Controls/Control.cs

@ -190,11 +190,11 @@ namespace Perspex.Controls
get { return Enumerable.Empty<ILogical>(); }
}
protected override void AttachedToVisualTree()
protected override void OnAttachedToVisualTree()
{
IStyler styler = Locator.Current.GetService<IStyler>();
styler.ApplyStyles(this);
base.AttachedToVisualTree();
base.OnAttachedToVisualTree();
}
protected void AddPseudoClass(PerspexProperty<bool> property, string className)

14
Perspex/Controls/DataTemplate.cs

@ -46,13 +46,23 @@ namespace Perspex.Controls
public class DataTemplate<T> : DataTemplate
{
public DataTemplate(Func<T, Control> build)
: base(typeof(T), o => build((T)o))
: base(typeof(T), Cast(build))
{
}
public DataTemplate(Func<T, bool> match, Func<T, Control> build)
: base(o => (o is T) ? match((T)o) : false, o => build((T)o))
: base(CastMatch(match), Cast(build))
{
}
private static Func<object, bool> CastMatch(Func<T, bool> f)
{
return o => (o is T) ? f((T)o) : false;
}
private static Func<object, TResult> Cast<TResult>(Func<T, TResult> f)
{
return o => f((T)o);
}
}
}

53
Perspex/Controls/TreeDataTemplate.cs

@ -26,16 +26,38 @@ namespace Perspex.Controls
{
}
public TreeDataTemplate(
Type type,
Func<object, Control> build,
Func<object, IEnumerable> itemsSelector,
Func<object, bool> isExpanded)
: this(o => IsInstance(o, type), build, itemsSelector, isExpanded)
{
}
public TreeDataTemplate(
Func<object, bool> match,
Func<object, Control> build,
Func<object, IEnumerable> itemsSelector)
: this(match, build, itemsSelector, _ => false)
{
this.ItemsSelector = itemsSelector;
}
public TreeDataTemplate(
Func<object, bool> match,
Func<object, Control> build,
Func<object, IEnumerable> itemsSelector,
Func<object, bool> isExpanded)
: base(match, build)
{
this.ItemsSelector = itemsSelector;
this.IsExpanded = isExpanded;
}
public Func<object, IEnumerable> ItemsSelector { get; private set; }
public Func<object, bool> IsExpanded { get; private set; }
}
public class TreeDataTemplate<T> : TreeDataTemplate
@ -43,7 +65,15 @@ namespace Perspex.Controls
public TreeDataTemplate(
Func<T, Control> build,
Func<T, IEnumerable> itemsSelector)
: base(typeof(T), o => build((T)o), o => itemsSelector((T)o))
: base(typeof(T), Cast(build), Cast(itemsSelector))
{
}
public TreeDataTemplate(
Func<T, Control> build,
Func<T, IEnumerable> itemsSelector,
Func<T, bool> isExpanded)
: base(typeof(T), Cast(build), Cast(itemsSelector), Cast(isExpanded))
{
}
@ -51,8 +81,27 @@ namespace Perspex.Controls
Func<T, bool> match,
Func<T, Control> build,
Func<T, IEnumerable> itemsSelector)
: base(o => (o is T) ? match((T)o) : false, o => build((T)o), o => itemsSelector((T)o))
: base(CastMatch(match), Cast(build), Cast(itemsSelector))
{
}
public TreeDataTemplate(
Func<T, bool> match,
Func<T, Control> build,
Func<T, IEnumerable> itemsSelector,
Func<T, bool> isExpanded)
: base(CastMatch(match), Cast(build), Cast(itemsSelector), Cast(isExpanded))
{
}
private static Func<object, bool> CastMatch(Func<T, bool> f)
{
return o => (o is T) ? f((T)o) : false;
}
private static Func<object, TResult> Cast<TResult>(Func<T, TResult> f)
{
return o => f((T)o);
}
}
}

1
Perspex/Controls/TreeView.cs

@ -25,6 +25,7 @@ namespace Perspex.Controls
{
Header = template.Build(item),
Items = template.ItemsSelector(item),
IsExpanded = template.IsExpanded(item),
};
}

20
Perspex/Controls/TreeViewItem.cs

@ -11,10 +11,30 @@ namespace Perspex.Controls
public static readonly PerspexProperty<bool> IsExpandedProperty =
PerspexProperty.Register<TreeViewItem, bool>("IsExpanded");
TreeView parent;
public bool IsExpanded
{
get { return this.GetValue(IsExpandedProperty); }
set { this.SetValue(IsExpandedProperty, value); }
}
protected override Control CreateItemControlOverride(object item)
{
if (this.parent != null)
{
return this.parent.CreateItemControl(item);
}
else
{
return null;
}
}
protected override void OnAttachedToVisualTree()
{
base.OnAttachedToVisualTree();
this.parent = this.GetVisualParent<Control>().TemplatedParent as TreeView;
}
}
}

22
Perspex/Visual.cs

@ -64,11 +64,11 @@ namespace Perspex
IVisual oldValue = this.visualParent;
this.visualParent = value;
this.InheritanceParent = (PerspexObject)value;
this.VisualParentChanged(oldValue, value);
this.OnVisualParentChanged(oldValue, value);
if (this.GetVisualAncestor<ILayoutRoot>() != null)
{
this.AttachedToVisualTree();
this.NotifyAttachedToVisualTree();
}
}
}
@ -104,21 +104,27 @@ namespace Perspex
Contract.Requires<ArgumentNullException>(context != null);
}
protected virtual void AttachedToVisualTree()
protected virtual void OnAttachedToVisualTree()
{
}
protected virtual void OnVisualParentChanged(IVisual oldValue, IVisual newValue)
{
}
private void NotifyAttachedToVisualTree()
{
this.Log().Debug(string.Format(
"Attached {0} (#{1:x8}) to visual tree",
this.GetType().Name,
this.GetHashCode()));
this.OnAttachedToVisualTree();
foreach (Visual child in ((IVisual)this).ExistingVisualChildren.OfType<Visual>())
{
child.AttachedToVisualTree();
child.NotifyAttachedToVisualTree();
}
}
protected virtual void VisualParentChanged(IVisual oldValue, IVisual newValue)
{
}
}
}

10
Perspex/VisualExtensions.cs

@ -126,6 +126,16 @@ namespace Perspex
}
}
public static IVisual GetVisualParent(this IVisual visual)
{
return visual.VisualParent;
}
public static T GetVisualParent<T>(this IVisual visual) where T : class
{
return visual.VisualParent as T;
}
public static IEnumerable<T> GetVisualSiblings<T>(this IVisual visual)
{
IVisual parent = visual.VisualParent;

3
TestApplication/Program.cs

@ -88,7 +88,8 @@ namespace TestApplication
{
new TreeDataTemplate<Node>(
x => new TextBlock { Text = x.Name },
x => x.Children),
x => x.Children,
x => true),
},
};

Loading…
Cancel
Save