From fc9382793a07126416d6fc80a79733a55334e41a Mon Sep 17 00:00:00 2001 From: sdoroff Date: Tue, 16 Oct 2018 15:00:44 -0400 Subject: [PATCH] Implement DataGridTemplateColumn --- .../DataGridTemplateColumn.cs | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/Avalonia.DataGrid/DataGridTemplateColumn.cs diff --git a/src/Avalonia.DataGrid/DataGridTemplateColumn.cs b/src/Avalonia.DataGrid/DataGridTemplateColumn.cs new file mode 100644 index 0000000000..c0f452af94 --- /dev/null +++ b/src/Avalonia.DataGrid/DataGridTemplateColumn.cs @@ -0,0 +1,69 @@ +// (c) Copyright Microsoft Corporation. +// This source is subject to the Microsoft Public License (Ms-PL). +// Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details. +// All other rights reserved. + +using Avalonia.Controls.Templates; +using Avalonia.Input; +using Avalonia.Interactivity; +using Avalonia.Media; +using Avalonia.Utilities; + +namespace Avalonia.Controls +{ + public class DataGridTemplateColumn : DataGridColumn + { + IDataTemplate _cellTemplate; + + public static readonly DirectProperty CellTemplateProperty = + AvaloniaProperty.RegisterDirect( + nameof(CellTemplate), + o => o.CellTemplate, + (o, v) => o.CellTemplate = v); + + public IDataTemplate CellTemplate + { + get { return _cellTemplate; } + set { SetAndRaise(CellTemplateProperty, ref _cellTemplate, value); } + } + + private void OnCellTemplateChanged(AvaloniaPropertyChangedEventArgs e) + { + //CellTemplateProperty.Changed.AddClassHandler(x => x.OnCellTemplateChanged); + var oldValue = (IDataTemplate)e.OldValue; + var value = (IDataTemplate)e.NewValue; + } + + public DataGridTemplateColumn() + { + IsReadOnly = true; + } + + protected override IControl GenerateElement(DataGridCell cell, object dataItem) + { + if(CellTemplate != null) + { + return CellTemplate.Build(dataItem); + } + if (Design.IsDesignMode) + { + return null; + } + else + { + throw DataGridError.DataGridTemplateColumn.MissingTemplateForType(typeof(DataGridTemplateColumn)); + } + } + + protected override IControl GenerateEditingElement(DataGridCell cell, object dataItem, out ICellEditBinding binding) + { + binding = null; + return GenerateElement(cell, dataItem); + } + + protected override object PrepareCellForEdit(IControl editingElement, RoutedEventArgs editingEventArgs) + { + return null; + } + } +}