<commit_before>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using SimpleDataGrid.ViewModel;
using System.Collections;
using System.Linq;

namespace SimpleDataGrid
{
    public class DataGridExt : DataGrid
    {
        public bool SkippedSelectionChangedEvent = false;

        public DataGridExt()
        {
            RowBackground = new SolidColorBrush(Colors.LightBlue);
            AlternatingRowBackground = new SolidColorBrush(Colors.LightGoldenrodYellow);
            AlternationCount = 2;
            CanUserSortColumns = false;
            CanUserReorderColumns = false;
            CanUserResizeRows = false;
            HeadersVisibility = DataGridHeadersVisibility.Column;

            KeepSelectionType = KeepSelection.KeepSelectedIndex;

            var menu = new ContextMenu();
            var menuItem = new MenuItem { Header = "Copy Data" };
            menuItem.Click += menuItem_Click;
            menu.Items.Add(menuItem);
            ContextMenu = menu;

            GotKeyboardFocus += DataGridExt_GotKeyboardFocus;
        }

        private void DataGridExt_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            var cell = e.NewFocus as DataGridCell;
            if (cell != null)
            {
                if (e.OldFocus is DataGridCell)
                {
                    var oldGrid = GetDataGridFromChild(e.OldFocus as DependencyObject);
                    var newGrid = GetDataGridFromChild(cell);
                    if (oldGrid == newGrid)
                    {
                        return;
                    }
                }

                if (_needUpdateSelectedIndexInGotFocusEvent)
                {
                    var index = Items.IndexOf(CurrentCell.Item);
                    if (SelectedIndex != index)
                    {
                        _needUpdateSelectedIndexInGotFocusEvent = false;
                        SelectedIndex = index;
                    }
                }
                else if ((SelectedIndex < Items.Count - 1) && SelectedIndex > 0)
                {
                    var index = Items.IndexOf(CurrentCell.Item);
                    if (SelectedIndex != index)
                    {
                        CurrentCell = new DataGridCellInfo(Items[SelectedIndex], Columns[1]);
                    }
                }
            }
        }

        private bool _needUpdateSelectedIndexInGotFocusEvent = false;

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Handled == false)
            {
                //SelectedIndex not update when move focus from header/footer to cell using Up/Down Arrow key
                // -> need update SelectedIndex base on current focused cell in GotFocus event
                if (Keyboard.IsKeyDown(Key.Down) || Keyboard.IsKeyDown(Key.Up))
                {
                    _needUpdateSelectedIndexInGotFocusEvent = true;
                }
            }
        }

        private DataGrid GetDataGridFromChild(DependencyObject child)
        {
            if (child == null)
            {
                return null;
            }
            var p = VisualTreeHelper.GetParent(child);
            var datagrid = p as DataGrid;
            if (datagrid != null)
            {
                return datagrid;
            }
            return GetDataGridFromChild(p);
        }

        public IEnumerable ItemsSourceEx
        {
            get { return (IEnumerable)GetValue(ItemsSourceExProperty); }
            set
            {<commit_msg>- use correct way to update CurrentCell when load (fix CurrentCell issue select by mouse).<commit_after>﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media;
using SimpleDataGrid.ViewModel;
using System.Collections;
using System.Linq;

namespace SimpleDataGrid
{
    public class DataGridExt : DataGrid
    {
        public bool SkippedSelectionChangedEvent = false;

        public DataGridExt()
        {
            RowBackground = new SolidColorBrush(Colors.LightBlue);
            AlternatingRowBackground = new SolidColorBrush(Colors.LightGoldenrodYellow);
            AlternationCount = 2;
            CanUserSortColumns = false;
            CanUserReorderColumns = false;
            CanUserResizeRows = false;
            HeadersVisibility = DataGridHeadersVisibility.Column;

            KeepSelectionType = KeepSelection.KeepSelectedIndex;

            var menu = new ContextMenu();
            var menuItem = new MenuItem { Header = "Copy Data" };
            menuItem.Click += menuItem_Click;
            menu.Items.Add(menuItem);
            ContextMenu = menu;

            GotKeyboardFocus += DataGridExt_GotKeyboardFocus;
            Loaded += DataGridExt_Loaded;
            Unloaded += DataGridExt_Unloaded;
        }

        private bool _isLoaded = false;
        private bool _needUpdateSelectedIndexInGotFocusEvent = false;
        private bool _needUpdateCurrentCellInGotFocusEvent = false;

        private void DataGridExt_Unloaded(object sender, RoutedEventArgs e)
        {
            _isLoaded = false;
        }

        private void DataGridExt_Loaded(object sender, RoutedEventArgs e)
        {
            //don't know why Loaded event fire two time, so need flag _isLoaded
            if (_isLoaded == true)
                return;

            _isLoaded = true;
            //CurrentCell not set to correct SelectedIndex when load -> need update CurrentCell in GotFocus
            _needUpdateCurrentCellInGotFocusEvent = true;
        }

        private void DataGridExt_GotKeyboardFocus(object sender, KeyboardFocusChangedEventArgs e)
        {
            var cell = e.NewFocus as DataGridCell;
            if (cell != null)
            {
                if (_needUpdateSelectedIndexInGotFocusEvent)
                {
                    _needUpdateSelectedIndexInGotFocusEvent = false;

                    var index = Items.IndexOf(CurrentCell.Item);
                    if (SelectedIndex != index)
                    {
                        SelectedIndex = index;
                    }
                }
                else if (_needUpdateCurrentCellInGotFocusEvent == true)
                {
                    _needUpdateCurrentCellInGotFocusEvent = false;

                    if ((SelectedIndex < Items.Count - 1) && SelectedIndex > 0)
                    {
                        var index = Items.IndexOf(CurrentCell.Item);
                        if (SelectedIndex != index)
                        {
                            CurrentCell = new DataGridCellInfo(Items[SelectedIndex], Columns[1]);
                        }
                    }
                }
            }
        }

        protected override void OnKeyDown(KeyEventArgs e)
        {
            base.OnKeyDown(e);

            if (e.Handled == false)
            {
                //SelectedIndex not update when move focus from header/footer to cell using Up/Down Arrow key
                // -> need update SelectedIndex base on current focused cell in GotFocus event
                if (Keyboard.IsKeyDown(Key.Down) || Keyboard.IsKeyDown(Key.Up))
                {
                    _needUpdateSelectedIndexInGotFocusEvent = true;
                }
            }
        }

        //private DataGrid GetDataGridFromChild(DependencyObject child)
        //{
        //    if (child == null)
        //    {
        //        return null;
        //    }
        //    var p = VisualTreeHelper.GetParent(child);
        //    var datagrid = p as DataGrid;
        //    if (datagrid != null)
        //    {
        //        return datagrid;
        //    }
        //    return GetDataGridFromChild(p);
        //}

        public IEnumerable ItemsSourceEx
        {
            get { return (IEnumerable)GetValue(ItemsSourceExProperty); }
            set
            {
{'fraction_non_alphanumeric': 0.06868834490317867, 'fraction_numerical': 0.0009743027645840945, 'mean_word_length': 1.5361334156886968, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 8, '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': '6411524', 'n_tokens_mistral': 2038, 'n_tokens_neox': 1911, 'n_words': 468}