<commit_before>﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Markup;

namespace MidiApp.Behaviors
{
    [ContentProperty(nameof(BehaviorCollection))]
    public abstract class CompositeBehavior<T> : Behavior<T>
        where T : DependencyObject
    {
        #region Behavior Collection

        private readonly HashSet<Type> _behaviors = new HashSet<Type>();

        public static readonly DependencyProperty BehaviorCollectionProperty =
            DependencyProperty.Register(
                $"{nameof(CompositeBehavior<T>)}<{typeof(T).Name}>",
                typeof(ObservableCollection<Behavior<T>>),
                typeof(CompositeBehavior<T>),
                new FrameworkPropertyMetadata(
                    null,
                    FrameworkPropertyMetadataOptions.NotDataBindable));

        public ObservableCollection<Behavior<T>> BehaviorCollection
        {
            get
            {
                var collection = GetValue(BehaviorCollectionProperty) as ObservableCollection<Behavior<T>>;

                if (collection == null)
                {
                    collection = new ObservableCollection<Behavior<T>>();
                    collection.CollectionChanged += OnCollectionChanged;
                    SetValue(BehaviorCollectionProperty, collection);
                }

                return collection;
            }
        }

        private void OnCollectionChanged(object sender, NotifyCollectionChangedEventArgs args)
        {
            if (args.OldItems != null)
            {
                foreach (var behavior in args.OldItems)
                {
                    if (_behaviors.Remove(behavior.GetType()) == false)
                    {
                        throw new InvalidOperationException($"{behavior.GetType().Name} could not be located.");
                    }
                }
            }

            if (args.NewItems != null)
            {
                foreach (var behavior in args.NewItems)
                {
                    if (_behaviors.Add(behavior.GetType()) == false)
                    {
                        throw new InvalidOperationException($"{behavior.GetType().Name} is set more than once.");
                    }
                }
            }
        }

        #endregion

        protected override void OnAttached()
        {
            foreach (var behavior in BehaviorCollection)
            {
                behavior.Attach(AssociatedObject);
            }<commit_msg>fixed binding problem on attached behaviors.<commit_after>﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Windows;
using System.Windows.Interactivity;
using System.Windows.Markup;

namespace MidiApp.Behaviors
{
    [ContentProperty(nameof(BehaviorCollection))]
    public abstract class CompositeBehavior<T> : Behavior<T>
        where T : DependencyObject
    {
        #region Behavior Collection
        
        public static readonly DependencyProperty BehaviorCollectionProperty =
            DependencyProperty.Register(
                $"{nameof(CompositeBehavior<T>)}<{typeof(T).Name}>",
                typeof(BehaviorCollection),
                typeof(CompositeBehavior<T>),
                new FrameworkPropertyMetadata(
                    null,
                    FrameworkPropertyMetadataOptions.NotDataBindable));

        public BehaviorCollection BehaviorCollection
        {
            get
            {
                var collection = GetValue(BehaviorCollectionProperty) as BehaviorCollection;

                if (collection == null)
                {
                    var constructor = typeof(BehaviorCollection)
                        .GetConstructor(
                            BindingFlags.NonPublic | BindingFlags.Instance,
                            null, Type.EmptyTypes, null);

                    collection = (BehaviorCollection) constructor.Invoke(null);
                    collection.Changed += OnCollectionChanged;
                    SetValue(BehaviorCollectionProperty, collection);
                }

                return collection;
            }
        }

        private void OnCollectionChanged(object sender, EventArgs eventArgs)
        {
            var hashset = new HashSet<Type>();

            foreach (var behavior in BehaviorCollection)
            {
                if (behavior is Behavior<T> == false)
                {
                    throw new InvalidOperationException($"{behavior.GetType().Name} does not inherit from {typeof(Behavior<T>).Name}.");
                }
                if (hashset.Add(behavior.GetType()) == false)
                {
                    throw new InvalidOperationException($"{behavior.GetType().Name} is set more than once.");
                }
            }
        }

        #endregion

        protected override void OnAttached()
        {
            foreach (var behavior in BehaviorCollection)
            {
                behavior.Attach(AssociatedObject);
            }
{'fraction_non_alphanumeric': 0.07883658629927287, 'fraction_numerical': 0.0, 'mean_word_length': 1.6943298969072165, 'pattern_counts': {'":': 0, '<': 26, '<?xml version=': 0, '>': 26, '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': '5434924', 'n_tokens_mistral': 1207, 'n_tokens_neox': 1101, 'n_words': 250}