<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using uEN.Core;
using uEN.UI.AttachedProperties;

namespace uEN.UI.AttachedProperties
{
    public delegate void SelectionChangingEventHandler(object sender, SelectionChangingEventargs e);
    public class SelectionChangingEventargs : RoutedEventArgs
    {
        public SelectionChangingEventargs(object oldItem, object newItem)
        {
            OldItem = oldItem;
            NewItem = NewItem;
        }
        public bool Cancel { get; set; }
        public object OldItem { get; private set; }
        public object NewItem { get; private set; }
    }

    public class SelectionChangingBehavior
    {
        public static readonly RoutedEvent SelectionChangingEvent =
            EventManager.RegisterRoutedEvent("SelectionChanging", RoutingStrategy.Direct,
            typeof(SelectionChangingEventHandler), typeof(SelectionChangingBehavior));
        public static SelectionChangingBehavior GetInstance(DependencyObject obj)
        {
            return (SelectionChangingBehavior)obj.GetValue(InstanceProperty);
        }
        public static void SetInstance(DependencyObject obj, SelectionChangingBehavior value)
        {
            obj.SetValue(InstanceProperty, value);
        }


        public static readonly DependencyProperty InstanceProperty =
            DependencyProperty.RegisterAttached("Instance", typeof(SelectionChangingBehavior), typeof(SelectionChangingBehavior),
            new FrameworkPropertyMetadata(null, OnInstanceChanged));
        private static void OnInstanceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var selector = d as Selector;
            if (selector == null) return;

            var oldValue = e.OldValue as SelectionChangingBehavior;
            if (oldValue != null)
            {
                oldValue.UnRegister(selector);
            }

            var newValue = e.NewValue as SelectionChangingBehavior;
            if (newValue != null)
            {
                newValue.Register(selector);
            }
        }
        public void Register(Selector selector)
        {
            selector.SelectionChanged += OnSelectionChanged;
        }
        public void UnRegister(Selector selector)
        {
            selector.SelectionChanged -= OnSelectionChanged;
        }
        void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (engaged) return;

            var selector = (Selector)sender;
            if (!selector.IsLoaded) return;

            engaged = true;
            try
            {
                var newItem = e.AddedItems.OfType<object>().FirstOrDefault();
                var oldItem = e.RemovedItems.OfType<object>().FirstOrDefault();

                //selector.SelectedItem = oldItem;
                selector.SetCurrentValue(Selector.SelectedItemProperty, oldItem);

                var eventArgs = new SelectionChangingEventargs(oldItem, newItem) { RoutedEvent = SelectionChangingEvent, };
                selector.RaiseEvent(eventArgs);
                if (!eventArgs.Cancel)
                {
                    //selector.SelectedItem = newItem;
                    selector.SetCurrentValue(Selector.SelectedItemProperty, newItem);
                }
            }
            finally
            {
                engaged = false;
            }
        }
        bool engaged = false;
    }
}
<commit_msg>bug fix<commit_after>﻿using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using uEN.Core;
using uEN.UI.AttachedProperties;

namespace uEN.UI.AttachedProperties
{
    public delegate void SelectionChangingEventHandler(object sender, SelectionChangingEventargs e);
    public class SelectionChangingEventargs : RoutedEventArgs
    {
        public SelectionChangingEventargs(object oldItem, object newItem)
        {
            OldItem = oldItem;
            NewItem = newItem;
        }
        public bool Cancel { get; set; }
        public object OldItem { get; private set; }
        public object NewItem { get; private set; }
    }

    public class SelectionChangingBehavior
    {
        public static readonly RoutedEvent SelectionChangingEvent =
            EventManager.RegisterRoutedEvent("SelectionChanging", RoutingStrategy.Direct,
            typeof(SelectionChangingEventHandler), typeof(SelectionChangingBehavior));
        public static SelectionChangingBehavior GetInstance(DependencyObject obj)
        {
            return (SelectionChangingBehavior)obj.GetValue(InstanceProperty);
        }
        public static void SetInstance(DependencyObject obj, SelectionChangingBehavior value)
        {
            obj.SetValue(InstanceProperty, value);
        }


        public static readonly DependencyProperty InstanceProperty =
            DependencyProperty.RegisterAttached("Instance", typeof(SelectionChangingBehavior), typeof(SelectionChangingBehavior),
            new FrameworkPropertyMetadata(null, OnInstanceChanged));
        private static void OnInstanceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            var selector = d as Selector;
            if (selector == null) return;

            var oldValue = e.OldValue as SelectionChangingBehavior;
            if (oldValue != null)
            {
                oldValue.UnRegister(selector);
            }

            var newValue = e.NewValue as SelectionChangingBehavior;
            if (newValue != null)
            {
                newValue.Register(selector);
            }
        }
        public void Register(Selector selector)
        {
            selector.SelectionChanged += OnSelectionChanged;
        }
        public void UnRegister(Selector selector)
        {
            selector.SelectionChanged -= OnSelectionChanged;
        }
        void OnSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (engaged) return;

            var selector = (Selector)sender;
            if (!selector.IsLoaded) return;

            engaged = true;
            try
            {
                var newItem = e.AddedItems.OfType<object>().FirstOrDefault();
                var oldItem = e.RemovedItems.OfType<object>().FirstOrDefault();

                //selector.SelectedItem = oldItem;
                selector.SetCurrentValue(Selector.SelectedItemProperty, oldItem);

                var eventArgs = new SelectionChangingEventargs(oldItem, newItem) { RoutedEvent = SelectionChangingEvent, };
                selector.RaiseEvent(eventArgs);
                if (!eventArgs.Cancel)
                {
                    //selector.SelectedItem = newItem;
                    selector.SetCurrentValue(Selector.SelectedItemProperty, newItem);
                }
            }
            finally
            {
                engaged = false;
            }
        }
        bool engaged = false;
    }
}

{'fraction_non_alphanumeric': 0.07356671740233384, 'fraction_numerical': 0.0, 'mean_word_length': 2.3186026936026938, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1787254', 'n_tokens_mistral': 1888, 'n_tokens_neox': 1837, 'n_words': 423}