示例#1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="MultiBindingLocalizedValue"/> class.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="resourceKey">The resource key.</param>
        /// <param name="stringFormat">The string format.</param>
        /// <param name="bindings">The bindings.</param>
        /// <exception cref="ArgumentNullException"><paramref name="property"/> is null.</exception>
        /// <exception cref="ArgumentException">The type of the <paramref name="property"/> is neither
        /// <see cref="String"/> nor <see cref="Object"/>.</exception>
        /// <exception cref="ArgumentException"><paramref name="resourceKey"/> is null or empty
        /// AND <paramref name="stringFormat"/> is null or empty.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="bindings"/> is null.</exception>
        /// <exception cref="ArgumentException"><paramref name="bindings"/> is empty.</exception>
        public MultiBindingLocalizedValue(
            LocalizedDependencyProperty property,
            string resourceKey,
            string stringFormat,
            ICollection <BindingBase> bindings
            )
            : base(property)
        {
            CheckPropertySupported(property);

            if (string.IsNullOrEmpty(resourceKey) && string.IsNullOrEmpty(stringFormat))
            {
                throw new ArgumentException("Either a resource key of a format string must be specified.");
            }

            if (bindings == null)
            {
                throw new ArgumentNullException("bindings");
            }

            if (bindings.Count == 0)
            {
                throw new ArgumentException("At least one binding must be specified.", "bindings");
            }

            _resourceKey = resourceKey;

            _stringFormat = stringFormat;

            _bindings = bindings;
        }
		/// <summary>
		/// Initializes a new instance of the <see cref="MultiBindingLocalizedValue"/> class.
		/// </summary>
		/// <param name="property">The property.</param>
		/// <param name="resourceKey">The resource key.</param>
		/// <param name="stringFormat">The string format.</param>
		/// <param name="bindings">The bindings.</param>
		/// <exception cref="ArgumentNullException"><paramref name="property"/> is null.</exception>
		/// <exception cref="ArgumentException">The type of the <paramref name="property"/> is neither
		/// <see cref="String"/> nor <see cref="Object"/>.</exception>
		/// <exception cref="ArgumentException"><paramref name="resourceKey"/> is null or empty
		/// AND <paramref name="stringFormat"/> is null or empty.</exception>
		/// <exception cref="ArgumentNullException"><paramref name="bindings"/> is null.</exception>
		/// <exception cref="ArgumentException"><paramref name="bindings"/> is empty.</exception>
		public MultiBindingLocalizedValue(
			LocalizedDependencyProperty property,
			string resourceKey,
			string stringFormat,
			ICollection<BindingBase> bindings
			)
			: base(property)
		{
			CheckPropertySupported(property);

			if (string.IsNullOrEmpty(resourceKey) && string.IsNullOrEmpty(stringFormat))
			{
				throw new ArgumentException("Either a resource key of a format string must be specified.");
			}

			if (bindings == null)
			{
				throw new ArgumentNullException("bindings");
			}

			if (bindings.Count == 0)
			{
				throw new ArgumentException("At least one binding must be specified.", "bindings");
			}

			_resourceKey = resourceKey;

			_stringFormat = stringFormat;

			_bindings = bindings;
		}
        /// <summary>
        /// Initializes a new instance of the <see cref="BindingLocalizedValue"/> class.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="binding">The cateory binding.</param>
        /// <param name="path">path to take resource key</param>
        public BindingLocalizedValue(
            LocalizedDependencyProperty property,
            Binding binding,
            string path
            )
            : base(property)
        {
            CheckPropertySupported(property);

            if (binding == null)
            {
                throw new ArgumentNullException("binding");
            }

            if (binding.Converter != null)
            {
                Property.Converter = binding.Converter;
                binding.Converter  = null;
            }

            // resolve resource key
            var b = new Binding(path)
            {
                Source = Property.Object.GetValue(FrameworkElement.DataContextProperty)
            };

            BindingOperations.SetBinding(_dummy, Dummy.ValueProperty, b);
            _resourceKey = _dummy.GetValue(Dummy.ValueProperty) as string;

            if (!string.IsNullOrEmpty(_resourceKey) && _resourceKey[0] == '^')
            {
                _resourceKey = _resourceKey.Substring(1);
                _category    = LocalizationScope.DefaultCategory;
            }
            else
            {
                // resolve resource category
                if (binding.Source == null)
                {
                    binding.Source = b.Source;
                }
                BindingOperations.SetBinding(_dummy, Dummy.ValueProperty, binding);
                _category = _dummy.GetValue(Dummy.ValueProperty) as string ?? LocalizationScope.DefaultCategory;
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="BindingLocalizedValue"/> class.
        /// </summary>
        /// <param name="property">The property.</param>
        /// <param name="binding">The cateory binding.</param>
        /// <param name="path">path to take resource key</param>
        public BindingLocalizedValue(
            LocalizedDependencyProperty property,
            Binding binding,
            string path
            )
            : base(property)
        {
            CheckPropertySupported(property);

            if (binding == null)
            {
                throw new ArgumentNullException("binding");
            }

            if (binding.Converter != null)
            {
                Property.Converter = binding.Converter;
                binding.Converter = null;
            }

            // resolve resource key
            var b = new Binding(path) { Source = Property.Object.GetValue(FrameworkElement.DataContextProperty) };
            BindingOperations.SetBinding(_dummy, Dummy.ValueProperty, b);
            _resourceKey = _dummy.GetValue(Dummy.ValueProperty) as string;

            if (!string.IsNullOrEmpty(_resourceKey) && _resourceKey[0] == '^')
            {
                _resourceKey = _resourceKey.Substring(1);
                _category = LocalizationScope.DefaultCategory;
            }
            else
            {
                // resolve resource category
                if (binding.Source == null) binding.Source = b.Source;
                BindingOperations.SetBinding(_dummy, Dummy.ValueProperty, binding);
                _category = _dummy.GetValue(Dummy.ValueProperty) as string ?? LocalizationScope.DefaultCategory;
            }
        }
示例#5
0
        /// <summary>
        /// When implemented in a derived class, returns an object that is set as the value of the target property for this markup extension.
        /// </summary>
        /// <param name="serviceProvider">Object 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)
        {
            var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;

            if (service == null)
            {
                return(null);
            }

            if (service.TargetObject is DependencyObject)
            {
                if (service.TargetProperty is DependencyProperty)
                {
                    var property = new LocalizedDependencyProperty(
                        (DependencyObject)service.TargetObject,
                        (DependencyProperty)service.TargetProperty
                        );

                    LocalizedValue localizedValue;

                    if (CategoryBinding != null)
                    {
                        localizedValue = new BindingLocalizedValue(property, CategoryBinding, StringFormat);
                    }
                    else
                    {
                        // Check if the property supports binding localization
                        MultiBindingLocalizedValue.CheckPropertySupported(property);

                        if (string.IsNullOrEmpty(ResourceKey) && string.IsNullOrEmpty(StringFormat))
                        {
                            // Either a resource key of a format string must be specified
                            return(null);
                        }

                        if (_bindings == null || _bindings.Count == 0)
                        {
                            // At least one binding must be specified
                            return(null);
                        }

                        localizedValue = new MultiBindingLocalizedValue(
                            property,
                            ResourceKey,
                            StringFormat,
                            _bindings
                            );
                    }

                    LocalizationManager.InternalAddLocalizedValue(localizedValue);

                    if (property.IsInDesignMode)
                    {
                        // At design time VS designer does not set the parent of any control
                        // before its properties are set. For this reason the correct values
                        // of inherited attached properties cannot be obtained.
                        // Therefore, to display the correct localized value it must be updated
                        // later ater the parrent of the control has been set.

                        ((DependencyObject)service.TargetObject).Dispatcher.BeginInvoke(
                            new SendOrPostCallback(x => ((LocalizedValue)x).UpdateValue()),
                            DispatcherPriority.ApplicationIdle,
                            localizedValue
                            );
                    }

                    return(localizedValue.GetValue());
                }
                else
                {
                    throw new Exception("This extension can be used only with dependency properties.");
                }
            }
            else if (service.TargetProperty is DependencyProperty || service.TargetProperty is PropertyInfo)
            {
                // The extension is used in a template

                return(this);
            }
            else
            {
                return(null);
            }
        }
示例#6
0
        /// <summary>
        /// When implemented in a derived class, returns an object that is set as the value of the target property for this markup extension.
        /// </summary>
        /// <param name="serviceProvider">Object 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)
        {
            var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;

            if (service == null)
            {
                return(null);
            }

            if (service.TargetObject is DependencyObject)
            {
                LocalizedProperty property;

                if (service.TargetProperty is DependencyProperty)
                {
                    property = new LocalizedDependencyProperty(
                        (DependencyObject)service.TargetObject,
                        (DependencyProperty)service.TargetProperty
                        );
                }
                else if (service.TargetProperty is PropertyInfo)
                {
                    property = new LocalizedNonDependencyProperty(
                        (DependencyObject)service.TargetObject,
                        (PropertyInfo)service.TargetProperty
                        );
                }
                else
                {
                    return(null);
                }

                property.Converter = Converter;

                property.ConverterParameter = ConverterParameter;

                var localizedValue = CreateLocalizedValue(property);

                if (localizedValue == null)
                {
                    return(null);
                }

                LocalizationManager.InternalAddLocalizedValue(localizedValue);

                if (property.IsInDesignMode)
                {
                    // At design time VS designer does not set the parent of any control
                    // before its properties are set. For this reason the correct values
                    // of inherited attached properties cannot be obtained.
                    // Therefore, to display the correct localized value it must be updated
                    // later ater the parrent of the control has been set.

                    ((DependencyObject)service.TargetObject).Dispatcher.BeginInvoke(
                        new SendOrPostCallback(x => ((LocalizedValue)x).UpdateValue()),
                        DispatcherPriority.ApplicationIdle,
                        localizedValue
                        );
                }

                return(localizedValue.GetValue());
            }
            else if (service.TargetProperty is DependencyProperty || service.TargetProperty is PropertyInfo)
            {
                // The extension is used in a template

                return(this);
            }
            else
            {
                return(null);
            }
        }
		/// <summary>
		/// When implemented in a derived class, returns an object that is set as the value of the target property for this markup extension.
		/// </summary>
		/// <param name="serviceProvider">Object 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)
		{
			var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;

			if (service == null)
			{
				return null;
			}

			if (service.TargetObject is DependencyObject)
			{
				LocalizedProperty property;

				if (service.TargetProperty is DependencyProperty)
				{
					property = new LocalizedDependencyProperty(
						(DependencyObject)service.TargetObject,
						(DependencyProperty)service.TargetProperty
						);
				}
				else if (service.TargetProperty is PropertyInfo)
				{
					property = new LocalizedNonDependencyProperty(
						(DependencyObject)service.TargetObject,
						(PropertyInfo)service.TargetProperty
						);
				}
				else
				{
					return null;
				}

				property.Converter = Converter;

				property.ConverterParameter = ConverterParameter;

				var localizedValue = CreateLocalizedValue(property);

				if (localizedValue == null)
				{
					return null;
				}

				LocalizationManager.InternalAddLocalizedValue(localizedValue);

				if (property.IsInDesignMode)
				{
					// At design time VS designer does not set the parent of any control
					// before its properties are set. For this reason the correct values
					// of inherited attached properties cannot be obtained.
					// Therefore, to display the correct localized value it must be updated
					// later ater the parrent of the control has been set.

					((DependencyObject)service.TargetObject).Dispatcher.BeginInvoke(
						new SendOrPostCallback(x => ((LocalizedValue)x).UpdateValue()),
						DispatcherPriority.ApplicationIdle,
						localizedValue
						);
				}

				return localizedValue.GetValue();
			}
			else if (service.TargetProperty is DependencyProperty || service.TargetProperty is PropertyInfo)
			{
				// The extension is used in a template

				return this;
			}
			else
			{
				return null;
			}
		}
        /// <summary>
        /// When implemented in a derived class, returns an object that is set as the value of the target property for this markup extension.
        /// </summary>
        /// <param name="serviceProvider">Object 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)
        {
            var service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget;

            if (service == null)
            {
                return null;
            }

            if (service.TargetObject is DependencyObject)
            {
                if (service.TargetProperty is DependencyProperty)
                {
                    var property = new LocalizedDependencyProperty(
                        (DependencyObject)service.TargetObject,
                        (DependencyProperty)service.TargetProperty
                        );

                    LocalizedValue localizedValue;

                    if (CategoryBinding != null)
                    {
                        localizedValue = new BindingLocalizedValue(property, CategoryBinding, StringFormat);
                    }
                    else
                    {
                        // Check if the property supports binding localization
                        MultiBindingLocalizedValue.CheckPropertySupported(property);

                        if (string.IsNullOrEmpty(ResourceKey) && string.IsNullOrEmpty(StringFormat))
                        {
                            // Either a resource key of a format string must be specified
                            return null;
                        }

                        if (_bindings == null || _bindings.Count == 0)
                        {
                            // At least one binding must be specified
                            return null;
                        }

                        localizedValue = new MultiBindingLocalizedValue(
                            property,
                            ResourceKey,
                            StringFormat,
                            _bindings
                            );
                    }

                    LocalizationManager.InternalAddLocalizedValue(localizedValue);

                    if (property.IsInDesignMode)
                    {
                        // At design time VS designer does not set the parent of any control
                        // before its properties are set. For this reason the correct values
                        // of inherited attached properties cannot be obtained.
                        // Therefore, to display the correct localized value it must be updated
                        // later ater the parrent of the control has been set.

                        ((DependencyObject)service.TargetObject).Dispatcher.BeginInvoke(
                            new SendOrPostCallback(x => ((LocalizedValue)x).UpdateValue()),
                            DispatcherPriority.ApplicationIdle,
                            localizedValue
                            );
                    }

                    return localizedValue.GetValue();
                }
                else
                {
                    throw new Exception("This extension can be used only with dependency properties.");
                }
            }
            else if (service.TargetProperty is DependencyProperty || service.TargetProperty is PropertyInfo)
            {
                // The extension is used in a template

                return this;
            }
            else
            {
                return null;
            }
        }