Ejemplo n.º 1
0
        /// <summary>
        /// When implemented in a derived class, returns an object that is provided as the value of the target property for this markup extension.
        /// </summary>
        /// <param name="serviceProvider">A service provider helper that can provide services for the markup extension.</param>
        /// <returns>
        /// The object value to set on the property where the extension is applied.
        /// </returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            object           property;
            FrameworkElement element;

            if (LocalizationBase.TryGetTarget(serviceProvider, out element, out property))
            {
                LocalizedString context = Localizer.GetString(this.Key);

                if (property is DependencyProperty)
                {
                    Application.Current.Dispatcher.BeginInvoke(
                        new Action(() => element.SetBinding(
                                       (DependencyProperty)property,
                                       new Binding
                    {
                        Source = context,
                        Path   = new PropertyPath("Value"),
                        Mode   = BindingMode.OneWay
                    })));
                }

                return(context.Value);
            }

            return(this);
        }
        /// <summary>
        /// Provides the value.
        /// </summary>
        /// <param name="serviceProvider">The service provider.</param>
        /// <returns>the value</returns>
        public override object ProvideValue(System.IServiceProvider serviceProvider)
        {
            object           property;
            FrameworkElement element;

            if (LocalizationBase.TryGetTarget(serviceProvider, out element, out property))
            {
                if (property is DependencyProperty)
                {
                    var context = new LocalizedBinding(element, this.PropertyPath);

                    Application.Current.Dispatcher.BeginInvoke(
                        new Action(() => element.SetBinding(
                                       (DependencyProperty)property,
                                       new Binding
                    {
                        Source = context,
                        Path   = new PropertyPath("Localized.Value"),
                        Mode   = BindingMode.OneWay
                    })));
                }

                var key = this.GetKey(element);

                if (!string.IsNullOrWhiteSpace(key))
                {
                    LocalizedString localized = Localizer.GetString(key);
                    return(localized.Value);
                }

                return(null);
            }

            return(this);
        }