/// <summary> /// Sets a binding between a <c>DependencyObject</c> with its<c>DependencyProperty</c> and this /// <c>BaseLocalizeExtension</c>. /// </summary> /// <param name="targetObject">The target dependency object.</param> /// <param name="targetProperty">The target dependency property.</param> /// <returns><c>True</c> if the binding was setup successfully, otherwise <c>False</c> (Binding already exists).</returns> public bool SetBinding(DependencyObject targetObject, DependencyProperty targetProperty) { // indicates, if the target object was found bool foundInWeakReferences = this.targetObjects.Any(wr => wr.Target == targetObject); // search for the target in the target object list // set the TargetProperty to the passed one this.TargetProperty = targetProperty; // if the target it's not collected already, collect it if (!foundInWeakReferences) { // if it's the first object, add an event handler too if (this.targetObjects.Count == 0) { // add this localize extension to the WeakEventManager on LocalizeDictionary OddsFormatManager.AddEventListener(this); } // add the target as an dependency object as weak reference to the dependency object list this.targetObjects.Add(new WeakReference(targetObject)); // adds this localize extension to the ObjectDependencyManager to ensure the lifetime along with the // target object ObjectDependencyManager.AddObjectDependency(new WeakReference(targetObject), this); string oddsString = GetLocalizedOddsString( this.displayValue, this.GetForcedOddsFormatOrDefault(), CultureInfo.CurrentCulture); // set the initial value of the dependency property targetObject.SetValue(this.TargetProperty, oddsString); // return true, the binding was successfully return(true); } // return false, the binding already exists return(false); }
/// <summary>Provides the Value for the first Binding.</summary> /// <param name="serviceProvider">The <c>IProvideValueTarget</c> provided from the <c>MarkupExtension</c>.</param> /// <returns>The found item from the .resx directory or <c>null</c> if not found.</returns> /// <remarks> /// This method register the <c>EventHandler</c><c>OnCultureChanged</c> on <c>LocalizeDictionary</c> to get an /// acknowledge of changing the culture, if the passed <c>TargetObjects</c> type of <see /// cref="DependencyObject" />. !PROOF: On every single <c>UserControl</c>, Window, and Page, there is a /// new SparedDP reference, and so there is every time a new <c>BaseLocalizeExtension</c>! Because of this, we /// don't need to notify every single DependencyObjects to update their value (for GC). /// </remarks> public override object ProvideValue(IServiceProvider serviceProvider) { if (serviceProvider == null) { throw new ArgumentNullException("serviceProvider"); } // try to cast the passed serviceProvider to a IProvideValueTarget var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; // if the cast fails, an exception will be thrown. (should never happen) if (service == null) { throw new InvalidOperationException("IProvideValueTarget service is unavailable"); } // if the service.TargetObject is a Binding, throw an exception if (service.TargetObject is Binding) { throw new InvalidOperationException("Use as binding is not supported!"); } // try to cast the service.TargetProperty as DependencyProperty this.TargetProperty = service.TargetProperty as DependencyProperty; // if this fails, return this (should never happen) if (this.TargetProperty == null) { return(this); } // indicates, if the target object was found bool foundInWeakReferences = this.targetObjects.Any(wr => wr.Target == service.TargetObject); // search for the target in the target object list // if the target is a dependency object and it's not collected already, collect it if (service.TargetObject is DependencyObject && !foundInWeakReferences) { // if it's the first object, add an event handler too if (this.targetObjects.Count == 0) { // add this localize extension to the WeakEventManager on LocalizeDictionary OddsFormatManager.AddEventListener(this); } // add the target as an dependency object as weak reference to the dependency object list this.targetObjects.Add(new WeakReference(service.TargetObject)); // adds this localize extension to the ObjectDependencyManager to ensure the lifetime along with the // target object ObjectDependencyManager.AddObjectDependency(new WeakReference(service.TargetObject), this); } // if the service.TargetObject is System.Windows.SharedDp (= not a DependencyObject), we return "this". the // SharedDp will call this instance later again. if (!(service.TargetObject is DependencyObject)) { // by returning "this", the provide value will be called later again. return(this); } // return the new value for the DependencyProperty return(GetLocalizedOddsString( this.displayValue, this.GetForcedOddsFormatOrDefault(), CultureInfo.CurrentCulture)); }