/// <summary> /// Sets a binding between a <c>DependencyObject</c> with its <c>DependencyProperty</c>or <see cref = /// "PropertyInfo" /> and the <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> /// <exception cref="ArgumentException">If the <paramref name="targetProperty" /> is not a <c>DependencyProperty</c> or <c>PropertyInfo</c>.</exception> public bool SetBinding(DependencyObject targetObject, object targetProperty) { if (!(targetProperty is DependencyProperty || targetProperty is PropertyInfo)) { throw new ArgumentException( "The targetProperty should be a DependencyProperty or PropertyInfo!", "targetProperty"); } // indicates, if the target object was found bool foundInWeakReferences = this.targetObjects.Any(wr => wr.Key.Target == targetObject && wr.Value == targetProperty); // search for the target in the target object list // 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 Localize.AddEventListener(this); } // add the target as an dependency object as weak reference to the dependency object list this.targetObjects.Add(new WeakReference(targetObject), targetProperty); // adds this localize extension to the ObjectDependencyManager to ensure the lifetime along with the // target object ObjectDependencyManager.AddObjectDependency(new WeakReference(targetObject), this); // get the initial value of the dependency property object output = this.FormatOutput( Localize.Instance.GetLocalizedObject <object>( this.Assembly, this.Dictionary, this.Key, this.Culture)); // set the value to the dependency object SetTargetValue(targetObject, targetProperty, output); // 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>System.Windows.Markup.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 <c>DependencyObject</c>. /// </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, return this if (service == null) { return(this); } // if the service.TargetObject is a Binding, throw an exception if (service.TargetObject is Binding) { throw new InvalidOperationException("Use as binding is not supported!"); } // declare a target property object targetProperty = null; // check if the service.TargetProperty is a DependencyProperty or a PropertyInfo if (service.TargetProperty is DependencyProperty || service.TargetProperty is PropertyInfo) { // set the target property to the service.TargetProperty targetProperty = service.TargetProperty; } // check if the target property is null if (targetProperty == null) { // return this. return(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) && !(service.TargetProperty is PropertyInfo)) { // by returning "this", the provide value will be called later again. return(this); } // indicates, if the target object was found bool foundInWeakReferences = this.targetObjects.Any( wr => wr.Key.Target == service.TargetObject && wr.Value == service.TargetProperty); // 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 Localize.AddEventListener(this); } // add the target as an dependency object as weak reference to the dependency object list this.targetObjects.Add(new WeakReference(service.TargetObject), service.TargetProperty); // adds this localize extension to the ObjectDependencyManager to ensure the lifetime along with the // target object ObjectDependencyManager.AddObjectDependency(new WeakReference(service.TargetObject), this); } // return the new value for the DependencyProperty return(Localize.Instance.GetLocalizedObject <object>(this.Assembly, this.Dictionary, this.Key, this.Culture)); }