/// <summary> /// Sets the value of the <strong>PropertyBinding</strong> attached dependency property. /// </summary> /// <param name="element">The element on which to set the property.</param> /// <param name="value"> /// The value for the <strong>PropertyBinding</strong> attached dependency property. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="element" /> is <see langword="null"/>. /// </exception> public static void SetPropertyBinding(DependencyObject element, SetterValueBindingHelper value) { if (element == null) { throw new ArgumentNullException("element"); } element.SetValue(PropertyBindingProperty, value); }
/// <summary> /// Applies the <see cref="Binding"/> represented by the <see cref="SetterValueBindingHelper"/>. /// </summary> /// <param name="element">The element to apply the <see cref="Binding"/> to.</param> /// <param name="item"> /// <see cref="SetterValueBindingHelper"/> representing the <see cref="Binding"/>. /// </param> /// <exception cref="ArgumentException"> /// <see cref="SetterValueBindingHelper"/>'s <see cref="Property"/> and <see cref="Binding"/> /// must both be set to non-null values. /// </exception> private static void ApplyBinding(FrameworkElement element, SetterValueBindingHelper item) { if ((item.Property == null) || (item.Binding == null)) throw new ArgumentException("SetterValueBindingHelper's Property and Binding must both be set to non-null values."); // Get the type on which to set the Binding. Type type = null; if (item.Type == null) { // No type specified; setting for the specified element. type = element.GetType(); } else { // Try to get the type from the type system. type = System.Type.GetType(item.Type); if (type == null) { // Search for the type in the list of assemblies foreach (var assembly in AssembliesToSearch) { // Match on short or full name type = assembly.GetTypes() .Where(t => (t.FullName == item.Type) || (t.Name == item.Type)) .FirstOrDefault(); if (type != null) { // Found; done searching break; } } if (type == null) { if (WindowsHelper.IsInDesignMode) return; // Unable to find the requested type anywhere string message = string.Format( CultureInfo.CurrentCulture, "Unable to access type \"{0}\". Try using an assembly qualified type name.", item.Type); throw new ArgumentException(message); } } } // Get the DependencyProperty for which to set the Binding DependencyProperty property = null; var field = type.GetField(item.Property + "Property", BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static); if (field != null) property = field.GetValue(null) as DependencyProperty; if (property == null) { if (WindowsHelper.IsInDesignMode) return; // Unable to find the requsted property string message = string.Format( CultureInfo.CurrentCulture, "Unable to access DependencyProperty \"{0}\" on type \"{1}\".", item.Property, type.Name); throw new ArgumentException(message); } // Set the specified Binding on the specified property element.SetBinding(property, item.Binding); }
/// <summary> /// Sets the value of the <strong>PropertyBinding</strong> attached dependency property. /// </summary> /// <param name="element">The element on which to set the property.</param> /// <param name="value"> /// The value for the <strong>PropertyBinding</strong> attached dependency property. /// </param> /// <exception cref="ArgumentNullException"> /// <paramref name="element" /> is <see langword="null"/>. /// </exception> public static void SetPropertyBinding(DependencyObject element, SetterValueBindingHelper value) { if (element == null) throw new ArgumentNullException("element"); element.SetValue(PropertyBindingProperty, value); }
/// <summary> /// Applies the <see cref="Binding"/> represented by the <see cref="SetterValueBindingHelper"/>. /// </summary> /// <param name="element">The element to apply the <see cref="Binding"/> to.</param> /// <param name="item"> /// <see cref="SetterValueBindingHelper"/> representing the <see cref="Binding"/>. /// </param> /// <exception cref="ArgumentException"> /// <see cref="SetterValueBindingHelper"/>'s <see cref="Property"/> and <see cref="Binding"/> /// must both be set to non-null values. /// </exception> private static void ApplyBinding(FrameworkElement element, SetterValueBindingHelper item) { if ((item.Property == null) || (item.Binding == null)) { throw new ArgumentException("SetterValueBindingHelper's Property and Binding must both be set to non-null values."); } // Get the type on which to set the Binding. Type type = null; if (item.Type == null) { // No type specified; setting for the specified element. type = element.GetType(); } else { // Try to get the type from the type system. type = System.Type.GetType(item.Type); if (type == null) { // Search for the type in the list of assemblies foreach (var assembly in AssembliesToSearch) { // Match on short or full name type = assembly.GetTypes() .Where(t => (t.FullName == item.Type) || (t.Name == item.Type)) .FirstOrDefault(); if (type != null) { // Found; done searching break; } } if (type == null) { if (WindowsHelper.IsInDesignMode) { return; } // Unable to find the requested type anywhere string message = string.Format( CultureInfo.CurrentCulture, "Unable to access type \"{0}\". Try using an assembly qualified type name.", item.Type); throw new ArgumentException(message); } } } // Get the DependencyProperty for which to set the Binding DependencyProperty property = null; var field = type.GetField(item.Property + "Property", BindingFlags.FlattenHierarchy | BindingFlags.Public | BindingFlags.Static); if (field != null) { property = field.GetValue(null) as DependencyProperty; } if (property == null) { if (WindowsHelper.IsInDesignMode) { return; } // Unable to find the requsted property string message = string.Format( CultureInfo.CurrentCulture, "Unable to access DependencyProperty \"{0}\" on type \"{1}\".", item.Property, type.Name); throw new ArgumentException(message); } // Set the specified Binding on the specified property element.SetBinding(property, item.Binding); }