/// <summary> /// This function returns the properly prepared output of the markup extension. /// </summary> public object FormatOutput() { object result = null; if (targetInfo == null) { return(null); } var targetObject = targetInfo.TargetObject as DependencyObject; // Get target type. Change ImageSource to BitmapSource in order to use our own converter. Type targetType = targetInfo.TargetPropertyType; if (targetType.Equals(typeof(System.Windows.Media.ImageSource))) { targetType = typeof(BitmapSource); } // Try to get the localized input from the resource. string resourceKey = this.Key; CultureInfo ci = GetForcedCultureOrDefault(); // Extract the names of the endpoint object and property string epName = ""; string epProp = ""; if (targetObject is FrameworkElement) { epName = (string)((FrameworkElement)targetObject).GetValue(FrameworkElement.NameProperty); } #if SILVERLIGHT #else else if (targetObject is FrameworkContentElement) { epName = (string)((FrameworkContentElement)targetObject).GetValue(FrameworkContentElement.NameProperty); } #endif if (targetInfo.TargetProperty is PropertyInfo) { epProp = ((PropertyInfo)targetInfo.TargetProperty).Name; } #if SILVERLIGHT else if (targetInfo.TargetProperty is DependencyProperty) { epProp = ((DependencyProperty)targetInfo.TargetProperty).ToString(); } #else else if (targetInfo.TargetProperty is DependencyProperty) { epProp = ((DependencyProperty)targetInfo.TargetProperty).Name; } #endif // What are these names during design time good for? Any suggestions? if (epProp.Contains("FrameworkElementWidth5")) { epProp = "Height"; } else if (epProp.Contains("FrameworkElementWidth6")) { epProp = "Width"; } else if (epProp.Contains("FrameworkElementMargin12")) { epProp = "Margin"; } string resKeyBase = ci.Name + ":" + targetType.Name + ":"; string resKeyNameProp = epName + LocalizeDictionary.GetSeparation(targetObject) + epProp; string resKeyName = epName; // Check, if the key is already in our resource buffer. object input = null; if (!String.IsNullOrEmpty(resourceKey)) { // We've got a resource key. Try to look it up or get it from the dictionary. if (ResourceBuffer.ContainsKey(resKeyBase + resourceKey)) { result = ResourceBuffer[resKeyBase + resourceKey]; } else { input = LocalizeDictionary.Instance.GetLocalizedObject(resourceKey, targetObject, ci); resKeyBase += resourceKey; } } else { // Try the automatic lookup function. // First, look for a resource entry named: [FrameworkElement name][Separator][Property name] if (ResourceBuffer.ContainsKey(resKeyBase + resKeyNameProp)) { result = ResourceBuffer[resKeyBase + resKeyNameProp]; } else { // It was not stored in the buffer - try to retrieve it from the dictionary. input = LocalizeDictionary.Instance.GetLocalizedObject(resKeyNameProp, targetObject, ci); if (input == null) { // Now, try to look for a resource entry named: [FrameworkElement name] // Note - this has to be nested here, as it would take precedence over the first step in the buffer lookup step. if (ResourceBuffer.ContainsKey(resKeyBase + resKeyName)) { result = ResourceBuffer[resKeyBase + resKeyName]; } else { input = LocalizeDictionary.Instance.GetLocalizedObject(resKeyName, targetObject, ci); resKeyBase += resKeyName; } } else { resKeyBase += resKeyNameProp; } } } // If no result was found, convert the input and add it to the buffer. if (result == null && input != null) { result = this.Converter.Convert(input, targetType, this.ConverterParameter, ci); ResourceBuffer.Add(resKeyBase, result); } return(result); }
/// <summary> /// This function returns the properly prepared output of the markup extension. /// </summary> /// <param name="info">Information about the target.</param> /// <param name="endPoint">Information about the endpoint.</param> public override object FormatOutput(TargetInfo endPoint, TargetInfo info) { object result = null; if (endPoint == null) { return(null); } else { lastEndpoint = SafeTargetInfo.FromTargetInfo(endPoint); } var targetObject = endPoint.TargetObject as DependencyObject; // Get target type. Change ImageSource to BitmapSource in order to use our own converter. var targetType = info.TargetPropertyType; if (targetType.Equals(typeof(System.Windows.Media.ImageSource))) { targetType = typeof(BitmapSource); } // In case of a list target, get the correct list element type. if ((info.TargetPropertyIndex != -1) && typeof(IList).IsAssignableFrom(info.TargetPropertyType)) { targetType = info.TargetPropertyType.GetGenericArguments()[0]; } // Try to get the localized input from the resource. var resourceKey = LocalizeDictionary.Instance.GetFullyQualifiedResourceKey(this.Key, targetObject); var ci = GetForcedCultureOrDefault(); // Extract the names of the endpoint object and property var epProp = GetPropertyName(endPoint.TargetProperty); var epName = ""; if (endPoint.TargetObject is FrameworkElement) { epName = ((FrameworkElement)endPoint.TargetObject).GetValueSync <string>(FrameworkElement.NameProperty); } else if (endPoint.TargetObject is FrameworkContentElement) { epName = ((FrameworkContentElement)endPoint.TargetObject).GetValueSync <string>(FrameworkContentElement.NameProperty); } var resKeyBase = ci.Name + ":" + targetType.Name + ":"; // Check, if the key is already in our resource buffer. object input = null; var isDefaultConverter = this.Converter is DefaultConverter; if (!String.IsNullOrEmpty(resourceKey)) { // We've got a resource key. Try to look it up or get it from the dictionary. if (isDefaultConverter && ResourceBuffer.ContainsKey(resKeyBase + resourceKey)) { result = ResourceBuffer[resKeyBase + resourceKey]; } else { input = LocalizeDictionary.Instance.GetLocalizedObject(resourceKey, targetObject, ci); resKeyBase += resourceKey; } } else { var resKeyNameProp = LocalizeDictionary.Instance.GetFullyQualifiedResourceKey(epName + LocalizeDictionary.GetSeparation(targetObject) + epProp, targetObject); // Try the automatic lookup function. // First, look for a resource entry named: [FrameworkElement name][Separator][Property name] if (isDefaultConverter && ResourceBuffer.ContainsKey(resKeyBase + resKeyNameProp)) { result = ResourceBuffer[resKeyBase + resKeyNameProp]; } else { // It was not stored in the buffer - try to retrieve it from the dictionary. input = LocalizeDictionary.Instance.GetLocalizedObject(resKeyNameProp, targetObject, ci); if (input == null) { var resKeyName = LocalizeDictionary.Instance.GetFullyQualifiedResourceKey(epName, targetObject); // Now, try to look for a resource entry named: [FrameworkElement name] // Note - this has to be nested here, as it would take precedence over the first step in the buffer lookup step. if (isDefaultConverter && ResourceBuffer.ContainsKey(resKeyBase + resKeyName)) { result = ResourceBuffer[resKeyBase + resKeyName]; } else { input = LocalizeDictionary.Instance.GetLocalizedObject(resKeyName, targetObject, ci); resKeyBase += resKeyName; } } else { resKeyBase += resKeyNameProp; } } } // If no result was found, convert the input and add it to the buffer. if (result == null) { if (input != null) { result = this.Converter.Convert(input, targetType, this.ConverterParameter, ci); if (isDefaultConverter) { SafeAddItemToResourceBuffer(resKeyBase, result); } } else { if (LocalizeDictionary.Instance.OnNewMissingKeyEvent(this, key)) { UpdateNewValue(); } if (!string.IsNullOrEmpty(key) && (targetType == typeof(String) || targetType == typeof(object))) { result = "Key: " + key; } } } return(result); }