internal void SetTemplateBinding(DependencyProperty dp, TemplateBindingExpression tb) { try { SetValue(dp, tb); } catch { // Do nothing here - The DP should still have its default value } }
/// <summary> /// Converts to a MarkupExtension /// </summary> public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType) { if (destinationType == typeof(MarkupExtension)) { TemplateBindingExpression templateBindingExpression = value as TemplateBindingExpression; if (templateBindingExpression == null) { throw new ArgumentException(SR.Get(SRID.MustBeOfType, "value", "TemplateBindingExpression")); } return(templateBindingExpression.TemplateBindingExtension); } return(base.ConvertTo(context, culture, value, destinationType)); }
public void SetTemplateBinding (TemplateBindingExpression tb, object obj) { DependencyObject dob = obj as DependencyObject; FrameworkElement fwe = obj as FrameworkElement; if (dob == null) throw Parser.ParseException ("Invalid TemplateBinding, expressions must be bound to DependendyObjects."); // Applying a {TemplateBinding} to a DO which is not a FrameworkElement should silently discard the binding. if (fwe == null) return; if (Parser.Context == null || Parser.Context.Template == null) throw Parser.ParseException ("Invalid TemplateBinding, expressions can not be used outside of FrameworkTemplate."); FrameworkElement source = Parser.Context.TemplateBindingSource; if (source == null) throw Parser.ParseException ("Invalid TemplateBinding, expression can not be used outside of a FrameworkTemplate."); // we don't actually use the type of the source to do property lookups. TargetType is used for that. // // For ControlTemplates: // 1. if there is a TargetType, we look up the property on TargetType. // 2. if there isn't a TargetType, we look up the property on typeof (Control). // For FrameworkTemplates: // there is no TargetType, so we only look up properties on typeof (FrameworkElement) // Type property_lookup_type; ControlTemplate template = Parser.Context.Template as ControlTemplate; if (template == null) return; // You can't apply Templatebindings outside of ControlTemplates property_lookup_type = template.TargetType ?? typeof (Control); DependencyProperty source_prop = LookupDependencyProperty (Deployment.Current.Types.TypeToKind (property_lookup_type), tb.SourcePropertyName); if (source_prop == null) throw Parser.ParseException ("Invalid TemplateBinding, property {0} could not be found.", tb.SourcePropertyName); DependencyProperty prop = LookupDependencyProperty (); if (prop == null) { var propInfo = property_lookup_type.GetProperty (tb.SourcePropertyName, XamlParser.PROPERTY_BINDING_FLAGS); if (!propInfo.PropertyType.IsInstanceOfType (tb)) { // If there is a CLR property with the correct name and there's no DP registered with that name // we should silently ignore the invalid TemplateBinding return; } throw Parser.ParseException ("Invalid TemplateBinding, property {0} could not be found.", Name); } tb.TargetProperty = prop; tb.SourceProperty = source_prop; // If we TemplateBind two incompatible properties we silently discard // the TemplateBinding object. Source properties of type object can // potentially be of the correct type, so we allow that too. if (source_prop.PropertyType == typeof (object) || prop.PropertyType.IsAssignableFrom (source_prop.PropertyType)) fwe.SetTemplateBinding (prop, tb); }
private void SetTemplateBinding (TemplateBindingExpression tb) { DependencyObject dob = target as DependencyObject; FrameworkElement fwe = target as FrameworkElement; if (dob == null) throw Parser.ParseException ("Invalid TemplateBinding, expressions must be bound to DependendyObjects."); // Applying a {TemplateBinding} to a DO which is not a FrameworkElement should silently discard the binding. if (fwe == null) return; if (Parser.Context == null || Parser.Context.Template == null) throw Parser.ParseException ("Invalid TemplateBinding, expressions can not be used outside of FrameworkTemplate."); FrameworkElement source = Parser.Context.TemplateBindingSource; if (source == null) throw Parser.ParseException ("Invalid TemplateBinding, expression can not be used outside of a FrameworkTemplate."); DependencyProperty source_prop = DependencyProperty.Lookup (source.GetKind(), tb.SourcePropertyName); if (source_prop == null) throw Parser.ParseException ("Invalid TemplateBinding, property {0} could not be found.", tb.SourcePropertyName); DependencyProperty prop = LookupDependencyProperty (); if (prop == null) throw Parser.ParseException ("Invalid TemplateBinding, property {0} could not be found.", Name); tb.TargetProperty = prop; tb.SourceProperty = source_prop; fwe.SetTemplateBinding (prop, tb); }
internal void SetTemplateBinding (DependencyProperty dp, TemplateBindingExpression tb) { try { SetValue (dp, tb); } catch { // Do nothing here - The DP should still have its default value } }
//+---------------------------------------------------------------------------------------------------------------- // // SetTemplateParentValues // // This method takes the "template parent values" (those that look like local values in the template), which // are ordinarily shared, and sets them as local values on the FE/FCE that was just created. This is used // during serialization. // //+---------------------------------------------------------------------------------------------------------------- internal static void SetTemplateParentValues( string name, object element, FrameworkTemplate frameworkTemplate, ref ProvideValueServiceProvider provideValueServiceProvider) { int childIndex; // Loop through the shared values, and set them onto the element. FrugalStructList<ChildRecord> childRecordFromChildIndex; HybridDictionary childIndexFromChildName; // Seal the template, and get the name->index and index->ChildRecord mappings if (!frameworkTemplate.IsSealed) { frameworkTemplate.Seal(); } childIndexFromChildName = frameworkTemplate.ChildIndexFromChildName; childRecordFromChildIndex = frameworkTemplate.ChildRecordFromChildIndex; // Calculate the child index childIndex = StyleHelper.QueryChildIndexFromChildName(name, childIndexFromChildName); // Do we have a ChildRecord for this index (i.e., there's some property set on it)? if (childIndex < childRecordFromChildIndex.Count) { // Yes, get the record. ChildRecord child = (ChildRecord)childRecordFromChildIndex[childIndex]; // Loop through the properties which are in some way set on this child for (int i = 0; i < child.ValueLookupListFromProperty.Count; i++) { // And for each of those properties, loop through the potential values specified in the template // for that property on that child. for (int j = 0; j < child.ValueLookupListFromProperty.Entries[i].Value.Count; j++) { // Get this value (in valueLookup) ChildValueLookup valueLookup; valueLookup = (ChildValueLookup)child.ValueLookupListFromProperty.Entries[i].Value.List[j]; // See if this value is one that is considered to be locally set on the child element if (valueLookup.LookupType == ValueLookupType.Simple || valueLookup.LookupType == ValueLookupType.Resource || valueLookup.LookupType == ValueLookupType.TemplateBinding) { // This shared value is for this element, so we'll set it. object value = valueLookup.Value; // If this is a TemplateBinding, put on an expression for it, so that it can // be represented correctly (e.g. for serialization). Otherwise, keep it as an ME. if (valueLookup.LookupType == ValueLookupType.TemplateBinding) { value = new TemplateBindingExpression(value as TemplateBindingExtension); } // Dynamic resources need to be converted to an expression also. else if (valueLookup.LookupType == ValueLookupType.Resource) { value = new ResourceReferenceExpression(value); } // Bindings are handled as just an ME // Set the value directly onto the element. MarkupExtension me = value as MarkupExtension; if (me != null) { // This is provided for completeness, but really there's only a few // MEs that survive TemplateBamlRecordReader. E.g. NullExtension would // have been converted to a null by now. There's only a few MEs that // are preserved, e.g. Binding and DynamicResource. Other MEs, such as // StaticResource, wouldn't be able to ProvideValue here, because we don't // have a ParserContext. if (provideValueServiceProvider == null) { provideValueServiceProvider = new ProvideValueServiceProvider(); } provideValueServiceProvider.SetData(element, valueLookup.Property); value = me.ProvideValue(provideValueServiceProvider); provideValueServiceProvider.ClearData(); } (element as DependencyObject).SetValue(valueLookup.Property, value); //sharedDp.Dp, value ); } } } } }
public object ParseTemplateBinding(ref string expression) { TemplateBindingExpression tb = new TemplateBindingExpression(); char next; string prop = GetNextPiece(ref expression, out next); tb.SourcePropertyName = prop; // tb.Source will be filled in elsewhere between attaching the change handler. return tb; }
public static DataTemplate CustomerComboBoxView(TemplateBindingExpression customer) { DataTemplate stuff = new DataTemplate(); return stuff; }
public void SetTemplateBinding (TemplateBindingExpression tb, object obj) { DependencyObject dob = obj as DependencyObject; FrameworkElement fwe = obj as FrameworkElement; if (dob == null) throw Parser.ParseException ("Invalid TemplateBinding, expressions must be bound to DependendyObjects."); // Applying a {TemplateBinding} to a DO which is not a FrameworkElement should silently discard the binding. if (fwe == null) return; if (Parser.Context == null || Parser.Context.Template == null) throw Parser.ParseException ("Invalid TemplateBinding, expressions can not be used outside of FrameworkTemplate."); FrameworkElement source = Parser.Context.TemplateBindingSource; if (source == null) throw Parser.ParseException ("Invalid TemplateBinding, expression can not be used outside of a FrameworkTemplate."); // we don't actually use the type of the source to do property lookups. TargetType is used for that. // // For ControlTemplates: // 1. if there is a TargetType, we look up the property on TargetType. // 2. if there isn't a TargetType, we look up the property on typeof (Control). // For FrameworkTemplates: // there is no TargetType, so we only look up properties on typeof (FrameworkElement) // Type property_lookup_type; ControlTemplate template = Parser.Context.Template as ControlTemplate; if (template == null) property_lookup_type = typeof (FrameworkElement); else property_lookup_type = template.TargetType ?? typeof (Control); DependencyProperty source_prop = LookupDependencyProperty (Deployment.Current.Types.TypeToKind (property_lookup_type), tb.SourcePropertyName); if (source_prop == null) throw Parser.ParseException ("Invalid TemplateBinding, property {0} could not be found.", tb.SourcePropertyName); DependencyProperty prop = LookupDependencyProperty (); if (prop == null) throw Parser.ParseException ("Invalid TemplateBinding, property {0} could not be found.", Name); tb.TargetProperty = prop; tb.SourceProperty = source_prop; fwe.SetTemplateBinding (prop, tb); }
internal DependencyPropertyChangedWeakListener(DependencyObject source, TemplateBindingExpression expression) { this.source = source; this.expression = new WeakReference(expression); this.source.DPChanged += this.SourcePropertyChanged; }