From 5cd27f7e8ffbabc1db6cdb017a8e8dc7ffe3438d Mon Sep 17 00:00:00 2001 From: Steven Kirk Date: Tue, 29 Sep 2015 23:56:31 +0200 Subject: [PATCH] Started implementing TreeDataTemplate. Binding system needs to be updated first. --- .../ViewModels/MainWindowViewModel.cs | 36 +++++++++++++ .../XamlTestApplication/Views/MainWindow.paml | 26 +++++++--- samples/XamlTestApplication/Views/TestNode.cs | 14 +++++ .../XamlTestApplication.csproj | 1 + .../Perspex.Markup.Xaml.csproj | 1 + .../Templates/DataTemplate.cs | 8 +-- .../Templates/TreeDataTemplate.cs | 52 +++++++++++++++++++ 7 files changed, 123 insertions(+), 15 deletions(-) create mode 100644 samples/XamlTestApplication/Views/TestNode.cs create mode 100644 src/Markup/Perspex.Markup.Xaml/Templates/TreeDataTemplate.cs diff --git a/samples/XamlTestApplication/ViewModels/MainWindowViewModel.cs b/samples/XamlTestApplication/ViewModels/MainWindowViewModel.cs index 75664f8188..1523299d41 100644 --- a/samples/XamlTestApplication/ViewModels/MainWindowViewModel.cs +++ b/samples/XamlTestApplication/ViewModels/MainWindowViewModel.cs @@ -15,8 +15,44 @@ namespace XamlTestApplication.ViewModels { Items.Add(new TestItem($"Item {i}", $"Item {i} Value")); } + + Nodes = new List + { + new TestNode + { + Header = "Root", + SubHeader = "Root Item", + Children = new[] + { + new TestNode + { + Header = "Child 1", + SubHeader = "Child 1 Value", + }, + new TestNode + { + Header = "Child 2", + SubHeader = "Child 2 Value", + Children = new[] + { + new TestNode + { + Header = "Grandchild", + SubHeader = "Grandchild Value", + }, + new TestNode + { + Header = "Grandmaster Flash", + SubHeader = "White Lines", + }, + } + }, + } + } + }; } public List Items { get; } + public List Nodes { get; } } } diff --git a/samples/XamlTestApplication/Views/MainWindow.paml b/samples/XamlTestApplication/Views/MainWindow.paml index b94dda826f..7fcf6bf749 100644 --- a/samples/XamlTestApplication/Views/MainWindow.paml +++ b/samples/XamlTestApplication/Views/MainWindow.paml @@ -60,16 +60,26 @@ - + + + + + + + + + + + + + - - - - - - + + - + + + diff --git a/samples/XamlTestApplication/Views/TestNode.cs b/samples/XamlTestApplication/Views/TestNode.cs new file mode 100644 index 0000000000..94c8fb6052 --- /dev/null +++ b/samples/XamlTestApplication/Views/TestNode.cs @@ -0,0 +1,14 @@ +// Copyright (c) The Perspex Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System.Collections.Generic; + +namespace XamlTestApplication.ViewModels +{ + public class TestNode + { + public string Header { get; set; } + public string SubHeader { get; set; } + public IEnumerable Children { get; set; } + } +} diff --git a/samples/XamlTestApplication/XamlTestApplication.csproj b/samples/XamlTestApplication/XamlTestApplication.csproj index c5b09898a6..28a5382d7e 100644 --- a/samples/XamlTestApplication/XamlTestApplication.csproj +++ b/samples/XamlTestApplication/XamlTestApplication.csproj @@ -80,6 +80,7 @@ + diff --git a/src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj b/src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj index 1f0e96020d..06eb0f4792 100644 --- a/src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj +++ b/src/Markup/Perspex.Markup.Xaml/Perspex.Markup.Xaml.csproj @@ -245,6 +245,7 @@ + diff --git a/src/Markup/Perspex.Markup.Xaml/Templates/DataTemplate.cs b/src/Markup/Perspex.Markup.Xaml/Templates/DataTemplate.cs index 9b1bf4731c..aa07179dac 100644 --- a/src/Markup/Perspex.Markup.Xaml/Templates/DataTemplate.cs +++ b/src/Markup/Perspex.Markup.Xaml/Templates/DataTemplate.cs @@ -12,7 +12,6 @@ namespace Perspex.Markup.Xaml.Templates public class DataTemplate : IDataTemplate { public Type DataType { get; set; } - public TemplateContent Content { get; set; } public bool Match(object data) @@ -25,12 +24,7 @@ namespace Perspex.Markup.Xaml.Templates return DataType == data.GetType(); } - public IControl Build(object param) - { - return CreateVisualTreeForItem(param); - } - - private Control CreateVisualTreeForItem(object data) + public IControl Build(object data) { var visualTreeForItem = Content.Load(); visualTreeForItem.DataContext = data; diff --git a/src/Markup/Perspex.Markup.Xaml/Templates/TreeDataTemplate.cs b/src/Markup/Perspex.Markup.Xaml/Templates/TreeDataTemplate.cs new file mode 100644 index 0000000000..e66d12ba75 --- /dev/null +++ b/src/Markup/Perspex.Markup.Xaml/Templates/TreeDataTemplate.cs @@ -0,0 +1,52 @@ +// Copyright (c) The Perspex Project. All rights reserved. +// Licensed under the MIT license. See licence.md file in the project root for full license information. + +using System; +using System.Collections; +using OmniXaml.Attributes; +using Perspex.Controls; +using Perspex.Controls.Templates; +using Perspex.Markup.Xaml.DataBinding; + +namespace Perspex.Markup.Xaml.Templates +{ + [ContentProperty("Content")] + public class TreeDataTemplate : ITreeDataTemplate + { + public Type DataType { get; set; } + public TemplateContent Content { get; set; } + public XamlBinding ItemsSource { get; set; } + + public bool Match(object data) + { + if (DataType == null) + { + throw new InvalidOperationException("DataTemplate must have a DataType."); + } + + return DataType == data.GetType(); + } + + public IEnumerable ItemsSelector(object item) + { + if (ItemsSource != null) + { + // TODO: Get value of ItemsSource here. + } + + return null; + } + + public bool IsExpanded(object item) + { + return true; + } + + public IControl Build(object data) + { + var visualTreeForItem = Content.Load(); + visualTreeForItem.DataContext = data; + return visualTreeForItem; + } + } +} \ No newline at end of file