<commit_before>﻿namespace Gu.Wpf.DataGrid2D
{
    using Internals;
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;

    public static partial class ItemsSource
    {
        public static readonly DependencyProperty Array2DProperty = DependencyProperty.RegisterAttached(
            "Array2D",
            typeof(Array),
            typeof(ItemsSource),
            new PropertyMetadata(default(Array), OnArray2DChanged),
            OnValidateArray2D);

        public static void SetArray2D(this DataGrid element, Array value)
        {
            element.SetValue(Array2DProperty, value);
        }

        [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)]
        [AttachedPropertyBrowsableForType(typeof(DataGrid))]
        public static Array GetArray2D(this DataGrid element)
        {
            return (Array)element.GetValue(Array2DProperty);
        }

        private static void OnArray2DChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dataGrid = (DataGrid)d;
            dataGrid.AutoGeneratingColumn -= DataGrid_AutoGeneratingColumn;
            var array = (Array)e.NewValue;
            if (array == null)
            {
                BindingOperations.ClearBinding(dataGrid, ItemsControl.ItemsSourceProperty);
                return;
            }

            var array2DView = Array2DView.Create(array);
            dataGrid.Bind(ItemsControl.ItemsSourceProperty)
                    .OneWayTo(array2DView);
            dataGrid.AutoGeneratingColumn += DataGrid_AutoGeneratingColumn;
            dataGrid.RaiseEvent(new RoutedEventArgs(Events.ColumnsChanged));
        }

        private static void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            CustomDataGridTemplateColumn col = new CustomDataGridTemplateColumn();
            col.CellTemplate = (DataTemplate)((DataGrid)sender).GetCellTemplate();
            col.CellEditingTemplate = (DataTemplate)((DataGrid)sender).GetCellEditingTemplate();
            if (col.CellTemplate != null && col.CellEditingTemplate != null)
            {
                DataGridTextColumn tc = e.Column as DataGridTextColumn;
                if (tc != null && tc.Binding != null)
                {
                    col.Binding = tc.Binding;
                    e.Column = col;
                }
            }
        }

        private static bool OnValidateArray2D(object value)
        {<commit_msg>Added two attached depency properties: CellTemplate and CellEditingTemlate. These can be used to specify a custom template for the cells of the colums of the 2D grid. It causes the grid to use TemplateColumns instead of TextColumns while autogenerating columns.<commit_after>﻿namespace Gu.Wpf.DataGrid2D
{
    using Internals;
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;

    public static partial class ItemsSource
    {
        public static readonly DependencyProperty Array2DProperty = DependencyProperty.RegisterAttached(
            "Array2D",
            typeof(Array),
            typeof(ItemsSource),
            new PropertyMetadata(default(Array), OnArray2DChanged),
            OnValidateArray2D);

        public static void SetArray2D(this DataGrid element, Array value)
        {
            element.SetValue(Array2DProperty, value);
        }

        [AttachedPropertyBrowsableForChildren(IncludeDescendants = false)]
        [AttachedPropertyBrowsableForType(typeof(DataGrid))]
        public static Array GetArray2D(this DataGrid element)
        {
            return (Array)element.GetValue(Array2DProperty);
        }

        private static void OnArray2DChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var dataGrid = (DataGrid)d;
            dataGrid.AutoGeneratingColumn -= DataGrid_AutoGeneratingColumn;
            var array = (Array)e.NewValue;
            if (array == null)
            {
                BindingOperations.ClearBinding(dataGrid, ItemsControl.ItemsSourceProperty);
                return;
            }

            var array2DView = Array2DView.Create(array);
            dataGrid.AutoGeneratingColumn += DataGrid_AutoGeneratingColumn;
            dataGrid.Bind(ItemsControl.ItemsSourceProperty)
                    .OneWayTo(array2DView);
            dataGrid.AutoGeneratingColumn += DataGrid_AutoGeneratingColumn;
            dataGrid.RaiseEvent(new RoutedEventArgs(Events.ColumnsChanged));
        }

        private static void DataGrid_AutoGeneratingColumn(object sender, DataGridAutoGeneratingColumnEventArgs e)
        {
            CustomDataGridTemplateColumn col = new CustomDataGridTemplateColumn();
            col.CellTemplate = (DataTemplate)((DataGrid)sender).GetCellTemplate();
            col.CellEditingTemplate = (DataTemplate)((DataGrid)sender).GetCellEditingTemplate();
            if (col.CellTemplate != null && col.CellEditingTemplate != null)
            {
                DataGridTextColumn tc = e.Column as DataGridTextColumn;
                if (tc != null && tc.Binding != null)
                {
                    col.Binding = tc.Binding;
                    e.Column = col;
                }
            }
        }

        private static bool OnValidateArray2D(object value)
        {
{'fraction_non_alphanumeric': 0.07272056109265411, 'fraction_numerical': 0.005352528608342562, 'mean_word_length': 2.2982349360925136, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6257637', 'n_tokens_mistral': 1366, 'n_tokens_neox': 1329, 'n_words': 281}