public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); _targetObject = target.TargetObject; _targetProperty = target.TargetProperty; Debug.Assert(_targetProperty != null || DesignerProperties.GetIsInDesignMode(new DependencyObject())); if (Resource.Binding == null && _targetProperty != null) { PropertyInfo propertyInfo = _targetProperty as PropertyInfo; if (propertyInfo != null) { try { return(Activator.CreateInstance(propertyInfo.PropertyType)); } catch (MissingMethodException) { //无构造引起的异常 } } DependencyProperty dpProperty = _targetProperty as DependencyProperty; if (dpProperty != null) { DependencyObject dpObj = (DependencyObject)_targetObject; return(dpObj.GetValue(dpProperty)); } } return(Resource.Binding); }
private static string AutoGenerateKey(IProvideValueTarget pvt) { var target = pvt.TargetObject as DependencyObject; var property = pvt.TargetProperty as DependencyProperty; return(Auto.GenerateKey(target, property)); }
protected virtual double GetVerticalSpace(IProvideValueTarget service) { if (_cachedVerticalSpace.HasValue) { return(_cachedVerticalSpace.Value); } try { FindAndCacheSpaceResources(service); } catch { if (IsInDesignMode) { return(DesignModeVerticalSpace); } throw; } if (!_cachedVerticalSpace.HasValue) { throw new Exception("Dimensions.VerticalSpace could not be retrieved."); } return(_cachedVerticalSpace.Value); }
public object ProvideValue(IServiceProvider serviceProvider) { if (serviceProvider == null) { throw new ArgumentNullException("serviceProvider"); } IRootObjectProvider rootObjectProvider = serviceProvider.GetService(typeof(IRootObjectProvider)) as IRootObjectProvider; if (rootObjectProvider == null) { throw new ArgumentException("serviceProvider does not provide an IRootObjectProvider"); } if (string.IsNullOrEmpty(this.Name)) { throw new ArgumentNullException("Name"); } Element nameScope = rootObjectProvider.RootObject as Element; Element element = nameScope.FindByName <Element>(this.Name); if (element == null) { throw new ArgumentNullException(string.Format("Can't find element named '{0}'", this.Name)); } object context = element.BindingContext; this.rootElement = element; IProvideValueTarget ipvt = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); this.attachedObject = ipvt.TargetObject as BindableObject; this.attachedObject.BindingContextChanged += this.OnContextChanged; return(context ?? new object()); }
/// <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) { IProvideValueTarget target = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); mTargetObject = target.TargetObject; mTargetProperty = target.TargetProperty; // mTargetProperty can be null when this is called in the Designer. Debug.Assert(mTargetProperty != null || DesignerProperties.GetIsInDesignMode(new DependencyObject())); if (DataResource.BindingTarget == null && mTargetProperty != null) { PropertyInfo propInfo = mTargetProperty as PropertyInfo; if (propInfo != null) { try { return(Activator.CreateInstance(propInfo.PropertyType)); } catch (MissingMethodException) { // there isn't a default constructor } } DependencyProperty depProp = mTargetProperty as DependencyProperty; if (depProp != null) { DependencyObject depObj = (DependencyObject)mTargetObject; return(depObj.GetValue(depProp)); } } return(DataResource.BindingTarget); }
public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; DependencyObject targetObject; DependencyProperty targetProperty; if (target != null && target.TargetObject is DependencyObject && target.TargetProperty is DependencyProperty) { targetObject = (DependencyObject)target.TargetObject; targetProperty = (DependencyProperty)target.TargetProperty; } else { return(this); // magic } // Bind the Param1 to attached property Param1BindingSinkProperty BindingOperations.SetBinding(targetObject, ParameterTemplateSelectorMarkupExtension.BindingSinkProperty, DictionaryBinding); // Now you can use Param1 // Param1 direct access example: IParameterTemplateDictionary param1Value = (IParameterTemplateDictionary)targetObject.GetValue(BindingSinkProperty); // Param1 use in binding example: var param1InnerBinding = new Binding() { Source = targetObject, Path = new PropertyPath("(0).SomeInnerProperty", BindingSinkProperty) }; // binding to Param1.SomeInnerProperty var binding = param1InnerBinding.ProvideValue(serviceProvider); // return binding to Param1.SomeInnerProperty return(new ParameterTemplateSelector(param1Value)); }
/// <summary> /// See <see cref="MarkupExtension.ProvideValue" /> /// </summary> public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; DependencyObject targetObject = null; DependencyProperty targetProperty; if (target != null && target.TargetObject is DependencyObject && target.TargetProperty is DependencyProperty) { targetObject = (DependencyObject)target.TargetObject; targetProperty = (DependencyProperty)target.TargetProperty; } else { return(this); } Binding binding = new Binding("Value"); if (this.DynamicKey != null && targetObject != null) { BindingOperations.SetBinding(targetObject, TranslateExtension.DynamicKeyBindingProperty, this.DynamicKey); binding.Source = new DynamicTranslationData(_staticKey, targetObject); } else { binding.Source = new TranslationData(_staticKey); } return(binding.ProvideValue(serviceProvider)); }
/// <summary> /// Returns the object that corresponds to the specified resource key. /// </summary> /// <param name="serviceProvider">An object that can provide services for the markup extension.</param> /// <returns>The object that corresponds to the specified resource key.</returns> public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (service != null && service.TargetProperty != null) { if (_locInstance == null) { _locInstance = LocalizationInstance.fromTargetProperty(service.TargetProperty); _locInstance.Key = Key; _locInstance.Format = Format; } if (service.TargetObject is DependencyObject) { _locInstance.AddTarget(service.TargetObject as DependencyObject); return(_locInstance.GetValue()); } else { //Localization is used in a Template return(this); } } return(null); }
/// <summary>Returns an object that should be set on the property where this extension is applied. For <see cref="T:System.Windows.ThemeDictionaryExtension" />, this is the URI value for a particular theme dictionary extension.</summary> /// <param name="serviceProvider">An object that can provide services for the markup extension. This service is expected to provide results for <see cref="T:System.Windows.Markup.IXamlTypeResolver" />.</param> /// <returns>The object value to set on the property where the extension is applied. </returns> /// <exception cref="T:System.InvalidOperationException">The <see cref="P:System.Windows.ThemeDictionaryExtension.AssemblyName" /> property is <see langword="null" />. You must set this value during construction or before using the <see cref="M:System.Windows.ThemeDictionaryExtension.ProvideValue(System.IServiceProvider)" /> method.-or- /// <paramref name="serviceProvide" />r is <see langword="null" /> or does not provide a service for <see cref="T:System.Windows.Markup.IXamlTypeResolver" />.-or- /// <paramref name="serviceProvider" /> specifies a target type that does not match <see cref="P:System.Windows.ResourceDictionary.Source" />.</exception> // Token: 0x06000C30 RID: 3120 RVA: 0x0002D554 File Offset: 0x0002B754 public override object ProvideValue(IServiceProvider serviceProvider) { if (string.IsNullOrEmpty(this.AssemblyName)) { throw new InvalidOperationException(SR.Get("ThemeDictionaryExtension_Name")); } IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (provideValueTarget == null) { throw new InvalidOperationException(SR.Get("MarkupExtensionNoContext", new object[] { base.GetType().Name, "IProvideValueTarget" })); } object targetObject = provideValueTarget.TargetObject; object targetProperty = provideValueTarget.TargetProperty; ResourceDictionary resourceDictionary = targetObject as ResourceDictionary; PropertyInfo left = targetProperty as PropertyInfo; if (resourceDictionary == null || (targetProperty != null && left != ThemeDictionaryExtension.SourceProperty)) { throw new InvalidOperationException(SR.Get("ThemeDictionaryExtension_Source")); } ThemeDictionaryExtension.Register(resourceDictionary, this._assemblyName); resourceDictionary.IsSourcedFromThemeDictionary = true; return(ThemeDictionaryExtension.GenerateUri(this._assemblyName, SystemResources.ResourceDictionaries.ThemedResourceName, UxThemeWrapper.ThemeName)); }
protected virtual string GetTargetPropertyName(IProvideValueTarget provideValueTarget, IServiceProvider serviceProvider) { //NOTE Xamarin doesn't support this property. //return serviceProvider.GetService<IProvideValueTarget>().TargetProperty; //http://forums.xamarin.com/discussion/36884/missing-implementation-of-iprovidevaluetarget-targetproperty-property-imarkupextension if (GetTargetPropertyNameDelegate != null) return GetTargetPropertyNameDelegate(provideValueTarget, serviceProvider); //Making some reflection magic. var xamlNode = GetValue(provideValueTarget, "Node", false); if (xamlNode == null) { var xamlNodeProvider = GetValue(serviceProvider, "IXamlNodeProvider"); xamlNode = GetValue(xamlNodeProvider, "XamlNode"); } var properties = (IDictionary)GetValue(GetValue(xamlNode, "Parent"), "Properties"); object xmlName = null; if (properties != null) { foreach (DictionaryEntry entry in properties) { if (ReferenceEquals(entry.Value, xamlNode)) { xmlName = entry.Key; break; } } } return (string)GetValue(xmlName, "LocalName"); }
void EnsureDelegateType(IServiceProvider serviceProvider) { if (DelegateType == null) { IProvideValueTarget valueTargetProvider = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); if (valueTargetProvider != null) { EventInfo eventInfo = valueTargetProvider.TargetProperty as EventInfo; if (eventInfo != null) { DelegateType = eventInfo.EventHandlerType; } else { PropertyInfo propertyInfo = valueTargetProvider.TargetProperty as PropertyInfo; if (propertyInfo != null) { DelegateType = propertyInfo.PropertyType; } } } if (DelegateType == null) { throw new InvalidOperationException( "No delegate type was specified and none could be inferred. You must specify a delegate type."); } } }
// This method only works with the C# 4.0 XamlParser. // If there was another way to do this without reflection... I would do it that way // Regardless, this method will only be called once when the xaml is initially parsed, so its // not really a performance issue. private static object GetInputBindingsCollectionOwner(IProvideValueTarget targetService) { var xamlContextField = targetService.GetType() .GetField("_xamlContext", BindingFlags.Instance | BindingFlags.NonPublic); if (xamlContextField == null) { return(null); } var xamlContext = xamlContextField.GetValue(targetService); var grandParentInstanceProperty = xamlContext.GetType().GetProperty("GrandParentInstance"); if (grandParentInstanceProperty == null) { return(null); } var inputBindingsCollection = grandParentInstanceProperty.GetGetMethod().Invoke(xamlContext, null); var ownerField = inputBindingsCollection.GetType() .GetField("_owner", BindingFlags.Instance | BindingFlags.NonPublic); if (ownerField == null) { return(null); } var owner = ownerField.GetValue(inputBindingsCollection); return(owner); }
/// <summary> /// Returns an object that should be set on the property where this extension is applied. /// </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 markup extension provided /// value is evaluated.</returns> /// <exception cref="System.InvalidOperationException">serviceProvider was null, or failed to /// implement a required service.</exception> public override object ProvideValue(IServiceProvider serviceProvider) { object obj = null; Type typeToResolve; if (_dataContextType == null) { typeToResolve = typeof(object); } else if (_dataContextType is Type) { typeToResolve = (Type)_dataContextType; } else if (_dataContextType is String) { #if !WINDOWS_UWP // ReSharper disable SuggestUseVarKeywordEvident IXamlTypeResolver service = serviceProvider.GetService(typeof(IXamlTypeResolver)) as IXamlTypeResolver; // ReSharper restore SuggestUseVarKeywordEvident typeToResolve = BindHelper.ResolveTypeByName((string)_dataContextType); if ((service != null) && (typeToResolve == null)) { typeToResolve = service.Resolve((string)_dataContextType); } #else typeToResolve = BindHelper.ResolveTypeByName((string)_dataContextType); #endif } else { throw new ArgumentNullException("LocateDataContext - DataContextType can be 'Type' or 'String' or empty."); } // ReSharper disable SuggestUseVarKeywordEvident IProvideValueTarget serviceProvideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; // ReSharper restore SuggestUseVarKeywordEvident if ((serviceProvideValueTarget != null) && (serviceProvideValueTarget.TargetObject != null)) { // Save targets for case delayed processing TargetObject = serviceProvideValueTarget.TargetObject; TargetProperty = serviceProvideValueTarget.TargetProperty; if (!(serviceProvideValueTarget.TargetObject is DependencyObject)) { throw new AggregateException("LocateDataContext - Target of the markup extension must be the DependencyObject type."); } obj = BindHelper.LocateValidDependencyPropertyByAllTrees(serviceProvideValueTarget.TargetObject as DependencyObject, FrameworkElement.DataContextProperty, MethodName, PropertyName, null, typeToResolve); if (obj == null) { var frameworkElement = TargetObject as FrameworkElement; if (frameworkElement != null) { frameworkElement.Loaded += DelayLocateDataContext; } } } return(obj); }
/// <summary> /// Return an object that should be set on the targetObject's targetProperty /// for this markup extension. For ThemeDictionaryExtension, this is the Uri /// pointing to theme specific dictionary in the specified assembly by AssemblyName. /// </summary> /// <param name="serviceProvider">ServiceProvider that can be queried for services.</param> /// <returns> /// The object to set on this property. /// </returns> public override object ProvideValue(IServiceProvider serviceProvider) { if (string.IsNullOrEmpty(AssemblyName)) { throw new InvalidOperationException(SR.Get(SRID.ThemeDictionaryExtension_Name)); } IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (provideValueTarget == null) { throw new InvalidOperationException(SR.Get(SRID.MarkupExtensionNoContext, GetType().Name, "IProvideValueTarget")); } object targetObject = provideValueTarget.TargetObject; object targetProperty = provideValueTarget.TargetProperty; ResourceDictionary dictionary = targetObject as ResourceDictionary; PropertyInfo propertyInfo = targetProperty as PropertyInfo; // Allow targetProperty to be null or ResourceDictionary.Source if (dictionary == null || (targetProperty != null && propertyInfo != SourceProperty)) { throw new InvalidOperationException(SR.Get(SRID.ThemeDictionaryExtension_Source)); } Register(dictionary, _assemblyName); dictionary.IsSourcedFromThemeDictionary = true; return(GenerateUri(_assemblyName, SystemResources.ResourceDictionaries.ThemedResourceName, MS.Win32.UxThemeWrapper.ThemeName)); }
protected virtual bool TryGetTargetItems(IServiceProvider provider, out DependencyObject target, out DependencyProperty dp) { target = null; dp = null; if (provider == null) { return(false); } //create a binding and assign it to the target IProvideValueTarget service = (IProvideValueTarget)provider.GetService(typeof(IProvideValueTarget)); if (service == null) { return(false); } target = service.TargetObject as DependencyObject; //we need dependency objects / properties #if SILVERLIGHT PropertyInfo propinfo = service.TargetProperty as PropertyInfo; dp = GetDependencyProperty(propinfo.DeclaringType, propinfo.Name); #else dp = service.TargetProperty as DependencyProperty; #endif return(target != null && dp != null); }
/// <summary> /// Get a source object for binding. /// If it is not set on, by default the method will search the first defined DataContext property value. /// </summary> /// <param name="serviceProvider">Object that can provide services for the markup extension.</param> /// <returns>Reference to a source object.</returns> protected override object ObtainSourceObject(IServiceProvider serviceProvider) { // For WinRT we provide a valid targets through BindXAML class. if (Source == null) { // ReSharper disable SuggestUseVarKeywordEvident IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; // ReSharper restore SuggestUseVarKeywordEvident if ((service != null) && (service.TargetObject != null)) { if (DeepScanAllTrees) { return(BindHelper.LocateValidDependencyPropertyByAllTrees(service.TargetObject as DependencyObject, FrameworkElement.DataContextProperty, ExecuteMethodName, ExecutePropertyName)); } else { return(BindHelper.LocateValidDependencyPropertyByAllTrees(service.TargetObject as DependencyObject, FrameworkElement.DataContextProperty)); } } } if (Source is string) { } return(Source); }
public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value) { string lookup = (string)value; object instance = null; IProvideValueTarget targetProvider = (IProvideValueTarget)context.GetService(typeof(IProvideValueTarget)); PackageItem targetItem = (PackageItem)targetProvider.TargetObject; string delayedLookupKey = null; DelayedItemLookup delayedLookup = null; PropertyInfo targetProperty = targetProvider.TargetProperty as PropertyInfo; if (targetProperty != null) { delayedLookupKey = targetProperty.Name; delayedLookup = new DelayedItemLookup(lookup, targetItem, targetProperty); } else // attached property { MethodInfo method = (MethodInfo)targetProvider.TargetProperty; ParameterInfo[] parameters = method.GetParameters(); // target type always comes from the second parameter. delayedLookupKey = String.Concat(method.DeclaringType.Namespace, ".", method.DeclaringType.Name, ".", method.Name, "()"); delayedLookup = new DelayedItemLookup(lookup, targetItem, parameters[1].ParameterType); } targetItem.DelayedLookup.Add(delayedLookupKey, delayedLookup); return(instance); }
public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget provideValue = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (provideValue != null) { var host = provideValue.TargetObject as FrameworkElement; var prop = provideValue.TargetProperty as DependencyProperty; } double result = 0; switch (OperationType) { case OperationType.Add: result = X + Y; break; case OperationType.Subtract: result = X - Y; break; case OperationType.Multiply: result = X * Y; break; case OperationType.Divide: result = Y == 0 ? 0 : X / Y; break; default: throw new ArgumentException("invalid operation "); } return(result.ToString()); }
public override object ProvideValue(IServiceProvider serviceProvider) { PropertyViewModel propertyViewModel; IProvideValueTarget target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (target == null) { return(null); } FrameworkElement targetObject = target.TargetObject as FrameworkElement; if (targetObject == null) { return(this); } propertyViewModel = targetObject.DataContext as PropertyViewModel; if (propertyViewModel?.AllowedValuesPath == null) { return(null); } DependencyProperty targetProperty = (DependencyProperty)target.TargetProperty; Binding binding = new Binding() { Path = new PropertyPath(propertyViewModel.AllowedValuesPath), Source = propertyViewModel.DataContext }; BindingOperations.SetBinding(targetObject, targetProperty, binding); return(binding.ProvideValue(serviceProvider)); }
public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget targetProvider = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (targetProvider != null) { var target = targetProvider.TargetObject as FrameworkElement; if (target != null) { if (targetProvider.TargetProperty is MethodInfo) { var targetEventAddMethod = targetProvider.TargetProperty as MethodInfo; if (targetEventAddMethod != null) { var delegateType = targetEventAddMethod.GetParameters()[1].ParameterType; var methodInfo = this.GetType().GetMethod("MyProxyHandler", BindingFlags.NonPublic | BindingFlags.Instance); return(Delegate.CreateDelegate(delegateType, this, methodInfo));; } } else if (targetProvider.TargetProperty is EventInfo) { var targetEventInfo = targetProvider.TargetProperty as EventInfo; if (targetEventInfo != null) { var delegateType = targetEventInfo.EventHandlerType; MethodInfo methodInfo = this.GetType().GetMethod("MyProxyHandler", BindingFlags.NonPublic | BindingFlags.Instance); return(Delegate.CreateDelegate(delegateType, this, methodInfo)); } } } } return(null); }
public object ProvideValue(IServiceProvider serviceProvider) { this.IsPortrait = DependencyService.Get <IDeviceOrientation>(DependencyFetchTarget.GlobalInstance).GetOrientation(); IProvideValueTarget provideValueTarget1 = serviceProvider != null?serviceProvider.GetService <IProvideValueTarget>() : (IProvideValueTarget)null; if (provideValueTarget1 == null) { throw new ArgumentException(); } IProvideValueTarget provideValueTarget2 = provideValueTarget1; if (this.UseBinding) { return (object)new Binding() { Source = (object)this, Path = "BindingValue" } } ; if (provideValueTarget2.TargetObject is Setter) { throw new NotSupportedException("The OnPlatform converters do not work with Setter.Value property"); } this.targetProperty = provideValueTarget2.TargetProperty as BindableProperty; this.targetObject = provideValueTarget2.TargetObject as BindableObject; return((object)this.BindingValue); } object IMarkupExtension.ProvideValue(IServiceProvider serviceProvider) { return(this.ProvideValue(serviceProvider)); }
public override object ProvideValue(IServiceProvider serviceProvider) { string key = ResourceKey; //Manuellen Schlüssel nehmen if (string.IsNullOrEmpty(key)) //Prüden ob manueller Key angegeben { //nein => dynamischer Key IProvideValueTarget targetProvider = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); FrameworkElement targetObject = targetProvider.TargetObject as FrameworkElement; string target = targetObject?.Name; if (string.IsNullOrEmpty(target)) { throw new Exception(nameof(targetProvider.TargetObject) + "must not be empty"); } string property = (targetProvider.TargetProperty as DependencyProperty)?.Name; if (string.IsNullOrEmpty(property)) { key = target; } else { key = target + "_" + property; } } return(ProvideResource(key)); }
static Func <object> GetContextProvider(IServiceProvider serviceProvider) { // If we can get our hands on a FrameworkElement, then we will only consider its // DataContext as a source for the environment. If we can't, we will consider the // template context, to give non-WPF code a chance. // IProvideValueTarget valueTargetProvider = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); if (valueTargetProvider != null && valueTargetProvider.TargetObject != null) { object target = valueTargetProvider.TargetObject; if (DataContextHelper.HasDataContext(target)) { return(() => { object finalDataContext = DataContextHelper.GetCurrentItem(DataContextHelper.GetDataContext(target)); return finalDataContext; }); } } SymbolResolver symbolResolver = serviceProvider.GetService(typeof(SymbolResolver)) as SymbolResolver; if (symbolResolver != null) { return(() => symbolResolver); } // No framework element // return(() => null); }
/// <summary> /// Returns a style that merges all styles with the keys specified in the constructor. /// </summary> /// <param name="serviceProvider">The service provider for this markup extension.</param> /// <returns>A style that merges all styles with the keys specified in the constructor.</returns> public override object ProvideValue(IServiceProvider serviceProvider) { Style resultStyle = new Style(); foreach (string currentResourceKey in resourceKeys) { object key = currentResourceKey; if (currentResourceKey == ".") { IProvideValueTarget service = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); if (service.TargetObject is Style) { key = ((Style)service.TargetObject).TargetType; } else { key = service.TargetObject.GetType(); } } Style currentStyle = new StaticResourceExtension(key).ProvideValue(serviceProvider) as Style; if (currentStyle == null) { throw new InvalidOperationException("Could not find style with resource key " + currentResourceKey + "."); } resultStyle.Merge(currentStyle); } return(resultStyle); }
public override object ProvideValue(IServiceProvider serviceProvider) { var borderClass = new BorderClass { Positioner = this }; IProvideValueTarget service = (IProvideValueTarget)serviceProvider.GetService(typeof(IProvideValueTarget)); borderClass.Border = service.TargetObject as FrameworkElement;; if (borderClass.Border == null) { return(this); } var cc = new CalcClass { BorderClass = borderClass, Popup = borderClass.Border.FindAncestor <Popup>() }; if (cc.Popup != null) { cc.Popup.Opened += cc.Openend; } return(new Thickness(1.0)); }
public object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget target = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; BindableObject obj = target.TargetObject as BindableObject; BindableProperty bindableProperty = target.TargetProperty as BindableProperty; if (obj != null && bindableProperty != null) { StringBuilder bindingBuilder = new StringBuilder($"{bindableProperty.PropertyName} {Binding}, Mode={Mode}"); if (!string.IsNullOrEmpty(Converter)) { bindingBuilder.Append($", Converter={Converter}"); } if (!string.IsNullOrEmpty(ConverterParameter)) { bindingBuilder.Append($", ConverterParameter={ConverterParameter}"); } if (!string.IsNullOrEmpty(FallbackValue)) { bindingBuilder.Append($", FallbackValue={FallbackValue}"); } obj.SetValue(Bi.ndProperty, bindingBuilder.ToString()); } else { Mvx.Trace(MvxTraceLevel.Diagnostic, "Cannot only use MvxBind on a bindable property"); } return(null); }
public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (service == null) { return(null); } FrameworkElement mTarget = service.TargetObject as FrameworkElement; DependencyProperty mProperty = service.TargetProperty as DependencyProperty; if (mTarget != null && mProperty != null) { var mDescriptor = DependencyPropertyDescriptor.FromProperty(mProperty, typeof(UIElement)); mDescriptor.AddValueChanged(mTarget, VisibilityChanged); RoutedEventHandler handler = null; handler = (s, e) => { mDescriptor.RemoveValueChanged(mTarget, VisibilityChanged); mTarget.Unloaded -= handler; }; mTarget.Unloaded += handler; if (_binding != null) { return(_binding.ProvideValue(serviceProvider));// BindingOperations.SetBinding(mTarget, mProperty, _Binding); } } return(Visibility.Visible); }
Type GetTargetType(IServiceProvider serviceProvider) { IProvideValueTarget provideValueTarget = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (provideValueTarget == null) { throw new NullReferenceException(); } DependencyObject targetObject = provideValueTarget.TargetObject as DependencyObject; if (targetObject == null) { throw new NullReferenceException(); } object targetProperty = provideValueTarget.TargetProperty; Type targetType = targetProperty.GetType(); if (targetProperty is DependencyProperty) { targetType = ((DependencyProperty)targetProperty).PropertyType; } return(targetType); }
public StyleSheetServiceProvider(object targetObject, object targetProperty) { vtProvider = new ValueTargetProvider { TargetObject = targetObject, TargetProperty = targetProperty }; }
public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget provideValue = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (provideValue == null) { return(this); } DependencyObject target = provideValue.TargetObject as DependencyObject; DependencyProperty property = provideValue.TargetProperty as DependencyProperty; // If not dependency object or dependency property, return this instance if (target == null || property == null) { return(this); } if (!ElementStateOperations.HasPropertyValue(target, property)) { object defaultValue = PropertyValueConverter.ConvertFromString(target.GetType(), property, Default); ElementStateOperations.AddPropertyValue(target, property, defaultValue); } BindingBase binding = ElementStateOperations.CreateBinding(target, property); object startValue = binding.ProvideValue(serviceProvider); return(startValue); }
public override object ProvideValue(IServiceProvider serviceProvider) { IProvideValueTarget service = serviceProvider.GetService(typeof(IProvideValueTarget)) as IProvideValueTarget; if (service == null) { throw new InvalidOperationException("IProvideValueTarget service is unavailable"); } DependencyProperty property = service.TargetProperty as DependencyProperty; if (property == null) { throw new ArgumentException("Target property must be of type DependencyProperty"); } DependencyObject target = service.TargetObject as DependencyObject; if (target == null) { return(this); } this._target = target; this._property = property; //FrameworkElement element = _target as FrameworkElement; //if (element != null) //{ // element.Loaded += element_Loaded; // element.Unloaded += element_Unloaded; //} //else //{ BindDictionary(); //} return(_target.GetValue(_property)); }
public ValueSerializerContext (PrefixLookup prefixLookup, XamlSchemaContext schemaContext, IAmbientProvider ambientProvider, IProvideValueTarget provideValue, IRootObjectProvider rootProvider) { if (prefixLookup == null) throw new ArgumentNullException ("prefixLookup"); if (schemaContext == null) throw new ArgumentNullException ("schemaContext"); prefix_lookup = prefixLookup; namespace_resolver = new NamespaceResolver (prefix_lookup.Namespaces); type_resolver = new XamlTypeResolver (namespace_resolver, schemaContext); sctx = schemaContext; ambient_provider = ambientProvider; this.provideValue = provideValue; this.rootProvider = rootProvider; }
// // This method only works with the C# 4.0 XamlParser. // If there was another way to do this without reflection... I would do it that way // Regardless, this method will only be called once when the xaml is initially parsed, so its // not really a performance issue. private static object GetInputBindingsCollectionOwner(IProvideValueTarget targetService) { var xamlContextField = targetService.GetType().GetField("_xamlContext", BindingFlags.Instance | BindingFlags.NonPublic); if (xamlContextField == null) { return null; } var xamlContext = xamlContextField.GetValue(targetService); var grandParentInstanceProperty = xamlContext.GetType().GetProperty("GrandParentInstance"); if (grandParentInstanceProperty == null) { return null; } var inputBindingsCollection = grandParentInstanceProperty.GetGetMethod().Invoke(xamlContext, null); var ownerField = inputBindingsCollection.GetType().GetField("_owner", BindingFlags.Instance | BindingFlags.NonPublic); if (ownerField == null) { return null; } var owner = ownerField.GetValue(inputBindingsCollection); return owner; }
public ServiceProviderDummy(IProvideValueTarget provideValueTargetContext) { _provideValueTargetContext = provideValueTargetContext; }
/// <summary> /// Checks if the given IProvideValueTarget can receive /// a DynamicResource or Binding MarkupExtension. /// </summary> internal static void CheckCanReceiveMarkupExtension( MarkupExtension markupExtension, IProvideValueTarget provideValueTarget, out DependencyObject targetDependencyObject, out DependencyProperty targetDependencyProperty) { targetDependencyObject = null; targetDependencyProperty = null; if (provideValueTarget == null) { return; } object targetObject = provideValueTarget.TargetObject; if (targetObject == null) { return; } Type targetType = targetObject.GetType(); object targetProperty = provideValueTarget.TargetProperty; if (targetProperty != null) { targetDependencyProperty = targetProperty as DependencyProperty; if (targetDependencyProperty != null) { // This is the DependencyProperty case targetDependencyObject = targetObject as DependencyObject; Debug.Assert(targetDependencyObject != null, "DependencyProperties can only be set on DependencyObjects"); } else { MemberInfo targetMember = targetProperty as MemberInfo; if (targetMember != null) { // This is the Clr Property case // Setters, Triggers, DataTriggers & Conditions are the special cases of // Clr properties where DynamicResource & Bindings are allowed. However // StyleHelper.ProcessSharedPropertyValue avoids a call to ProvideValue // in these cases and so there is no need for special code to handle them here. // Find the MemberType Debug.Assert(targetMember is PropertyInfo || targetMember is MethodInfo, "TargetMember is either a Clr property or an attached static settor method"); Type memberType; PropertyInfo propertyInfo = targetMember as PropertyInfo; if (propertyInfo != null) { memberType = propertyInfo.PropertyType; } else { MethodInfo methodInfo = (MethodInfo)targetMember; ParameterInfo[] parameterInfos = methodInfo.GetParameters(); Debug.Assert(parameterInfos.Length == 2, "The signature of a static settor must contain two parameters"); memberType = parameterInfos[1].ParameterType; } // Check if the MarkupExtensionType is assignable to the given MemberType // This check is to allow properties such as the following // - DataTrigger.Binding // - Condition.Binding // - HierarchicalDataTemplate.ItemsSource // - GridViewColumn.DisplayMemberBinding if (!typeof(MarkupExtension).IsAssignableFrom(memberType) || !memberType.IsAssignableFrom(markupExtension.GetType())) { throw new XamlParseException(SR.Get(SRID.MarkupExtensionDynamicOrBindingOnClrProp, markupExtension.GetType().Name, targetMember.Name, targetType.Name)); } } else { // This is the Collection ContentProperty case // Example: // <DockPanel> // <Button /> // <DynamicResource ResourceKey="foo" /> // </DockPanel> // Collection<BindingBase> used in MultiBinding is a special // case of a Collection that can contain a Binding. if (!typeof(BindingBase).IsAssignableFrom(markupExtension.GetType()) || !typeof(Collection<BindingBase>).IsAssignableFrom(targetProperty.GetType())) { throw new XamlParseException(SR.Get(SRID.MarkupExtensionDynamicOrBindingInCollection, markupExtension.GetType().Name, targetProperty.GetType().Name)); } } } } else { // This is the explicit Collection Property case // Example: // <DockPanel> // <DockPanel.Children> // <Button /> // <DynamicResource ResourceKey="foo" /> // </DockPanel.Children> // </DockPanel> // Collection<BindingBase> used in MultiBinding is a special // case of a Collection that can contain a Binding. if (!typeof(BindingBase).IsAssignableFrom(markupExtension.GetType()) || !typeof(Collection<BindingBase>).IsAssignableFrom(targetType)) { throw new XamlParseException(SR.Get(SRID.MarkupExtensionDynamicOrBindingInCollection, markupExtension.GetType().Name, targetType.Name)); } } }