// (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.Controls.Utils; using Avalonia.Input; using Avalonia.Interactivity; using Avalonia.Media; using Avalonia.Metadata; using Avalonia.Utilities; namespace Avalonia.Controls { public class DataGridTemplateColumn : DataGridColumn { private IDataTemplate _cellTemplate; public static readonly DirectProperty CellTemplateProperty = AvaloniaProperty.RegisterDirect( nameof(CellTemplate), o => o.CellTemplate, (o, v) => o.CellTemplate = v); [Content] public IDataTemplate CellTemplate { get { return _cellTemplate; } set { SetAndRaise(CellTemplateProperty, ref _cellTemplate, value); } } private IDataTemplate _cellEditingCellTemplate; /// /// Defines the property. /// public static readonly DirectProperty CellEditingTemplateProperty = AvaloniaProperty.RegisterDirect( nameof(CellEditingTemplate), o => o.CellEditingTemplate, (o, v) => o.CellEditingTemplate = v); /// /// Gets or sets the which is used for the editing mode of the current /// /// /// An for the editing mode of the current /// /// /// If this property is the column is read-only. /// public IDataTemplate CellEditingTemplate { get => _cellEditingCellTemplate; set => SetAndRaise(CellEditingTemplateProperty, ref _cellEditingCellTemplate, value); } private static void OnCellTemplateChanged(AvaloniaPropertyChangedEventArgs e) { var oldValue = (IDataTemplate)e.OldValue; var value = (IDataTemplate)e.NewValue; } protected override Control GenerateElement(DataGridCell cell, object dataItem) { if (CellTemplate != null) { return (CellTemplate is IRecyclingDataTemplate recyclingDataTemplate) ? recyclingDataTemplate.Build(dataItem, cell.Content as Control) : CellTemplate.Build(dataItem); } if (Design.IsDesignMode) { return null; } else { throw DataGridError.DataGridTemplateColumn.MissingTemplateForType(typeof(DataGridTemplateColumn)); } } protected override Control GenerateEditingElement(DataGridCell cell, object dataItem, out ICellEditBinding binding) { binding = null; if(CellEditingTemplate != null) { return CellEditingTemplate.Build(dataItem); } else if (CellTemplate != null) { return CellTemplate.Build(dataItem); } if (Design.IsDesignMode) { return null; } else { throw DataGridError.DataGridTemplateColumn.MissingTemplateForType(typeof(DataGridTemplateColumn)); } } protected override object PrepareCellForEdit(Control editingElement, RoutedEventArgs editingEventArgs) { return null; } protected internal override void RefreshCellContent(Control element, string propertyName) { var cell = element.Parent as DataGridCell; if(propertyName == nameof(CellTemplate) && cell is not null) { cell.Content = GenerateElement(cell, cell.DataContext); } base.RefreshCellContent(element, propertyName); } public override bool IsReadOnly { get { if (CellEditingTemplate is null) { return true; } return base.IsReadOnly; } set { base.IsReadOnly = value; } } } }