/// <summary>
        /// Returns the object that corresponds to the specified resource key.
        /// </summary>
        /// <param name="serviceProvider">An object that can provide services for the markup extension.</param>
        /// <returns>The object that corresponds to the specified resource key.</returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;

            if (service != null && service.TargetProperty != null)
            {
                if (_locInstance == null)
                {
                    _locInstance        = LocalizationInstance.fromTargetProperty(service.TargetProperty);
                    _locInstance.Key    = Key;
                    _locInstance.Format = Format;
                }
                if (service.TargetObject is DependencyObject)
                {
                    _locInstance.AddTarget(service.TargetObject as DependencyObject);
                    return(_locInstance.GetValue());
                }
                else
                {
                    //Localization is used in a Template
                    return(this);
                }
            }
            return(null);
        }
        /// <summary>
        /// Adds the specified localization instance to the list of manages localization instances.
        /// </summary>
        /// <param name="localization">The localization instance.</param>
        internal static void AddLocalization(LocalizationInstance localization)
        {
            if (localization == null)
            {
                throw new ArgumentNullException("localization");
            }

            if (_localizationPurgeCount > 50)
            {
                // It's really faster to fill a new list instead of removing elements
                // from the existing list when there are a lot of elements to remove.

                var localizatons = new List <LocalizationInstance>(_localizations.Count);

                foreach (var item in _localizations)
                {
                    if (item.IsAlive)
                    {
                        localizatons.Add(item);
                    }
                }

                _localizations = localizatons;

                _localizationPurgeCount = 0;
            }

            _localizations.Add(localization);

            _localizationPurgeCount++;
        }
        public LocBindingTarget(LocalizationInstance loc, FrameworkElement targetObject, PropertyPath path)
        {
            _locInstance = loc;
            _lbCollection.Add(this);
            _path = path;


            targetObject.DataContextChanged += new DependencyPropertyChangedEventHandler(update_binding);

            update_binding(targetObject, new DependencyPropertyChangedEventArgs(FrameworkElement.DataContextProperty, null, targetObject.DataContext));
        }
        public LocBindingTarget(LocalizationInstance loc, FrameworkElement targetObject, PropertyPath path)
        {
            _locInstance = loc;
            _lbCollection.Add(this);
            _path = path;
            
            
            targetObject.DataContextChanged += new DependencyPropertyChangedEventHandler(update_binding);

            update_binding(targetObject, new DependencyPropertyChangedEventArgs(FrameworkElement.DataContextProperty, null, targetObject.DataContext));

        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
            if (service != null && service.TargetProperty != null)
            {
                var targetObject = service.TargetObject as FrameworkElement;
                if (targetObject != null)
                {
                    _locInstance = LocalizationInstance.fromTargetProperty(service.TargetProperty);
                    _locInstance.AddTarget(targetObject);

                    var bTarget = new LocBindingTarget(_locInstance, targetObject, Path);

                    return _locInstance.GetValue();
                }
                else
                    return this;
            }
            return null;
        }
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;

            if (service != null && service.TargetProperty != null)
            {
                var targetObject = service.TargetObject as FrameworkElement;
                if (targetObject != null)
                {
                    _locInstance = LocalizationInstance.fromTargetProperty(service.TargetProperty);
                    _locInstance.AddTarget(targetObject);

                    var bTarget = new LocBindingTarget(_locInstance, targetObject, Path);

                    return(_locInstance.GetValue());
                }
                else
                {
                    return(this);
                }
            }
            return(null);
        }
 /// <summary>
 /// Returns the object that corresponds to the specified resource key.
 /// </summary>
 /// <param name="serviceProvider">An object that can provide services for the markup extension.</param>
 /// <returns>The object that corresponds to the specified resource key.</returns>
 public override object ProvideValue(IServiceProvider serviceProvider)
 {
     IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;
     if (service != null && service.TargetProperty != null)
     {
         if (_locInstance == null)
         {
             _locInstance = LocalizationInstance.fromTargetProperty(service.TargetProperty);
             _locInstance.Key = Key;
             _locInstance.Format = Format;
         }
         if (service.TargetObject is DependencyObject)
         {
             _locInstance.AddTarget(service.TargetObject as DependencyObject);
             return _locInstance.GetValue();
         }
         else
         {
             //Localization is used in a Template
             return this;
         }
     }
     return null;
 }
        /// <summary>
        /// Adds the specified localization instance to the list of manages localization instances.
        /// </summary>
        /// <param name="localization">The localization instance.</param>
        internal static void AddLocalization(LocalizationInstance localization)
        {
            if (localization == null)
            {
                throw new ArgumentNullException("localization");
            }

            if (_localizationPurgeCount > 50)
            {
                // It's really faster to fill a new list instead of removing elements
                // from the existing list when there are a lot of elements to remove.

                var localizatons = new List<LocalizationInstance>(_localizations.Count);

                foreach (var item in _localizations)
                {
                    if (item.IsAlive)
                    {
                        localizatons.Add(item);
                    }
                }

                _localizations = localizatons;

                _localizationPurgeCount = 0;
            }

            _localizations.Add(localization);

            _localizationPurgeCount++;
        }