public override void SetValue(NativeView view, object value) { switch (value) { case null: view.SetValue(DependencyProperty, null); break; case NativeBinding binding: binding.Mode = IsTwoWay ? BindingMode.TwoWay : BindingMode.OneWay; view.SetBinding(DependencyProperty, binding); break; default: if (!ValueType.IsEnum) { value.TryChangeType(ValueType, out value); } value = Converter == null ? value : Converter.Invoke(value); if (value.GetType() == PropertyType) { view.SetValue(DependencyProperty, value); } break; } }
/// <summary> /// Sets a binding from code /// </summary> /// <param name="element"></param> /// <param name="property"></param> /// <param name="source"></param> /// <param name="path"></param> /// <param name="converter"></param> public static void SetBinding(FrameworkElement element, DependencyProperty property, object source, string path, IValueConverter converter = null) { Binding binding = new Binding(); binding.Source = source; binding.Path = new PropertyPath(path); binding.Converter = converter; element.SetBinding(property, binding); }
public static void BindProperty(FrameworkElement element, object source, string path, DependencyProperty property, BindingMode mode) { var binding = new Binding(); binding.Path = new PropertyPath(path); binding.Source = source; binding.Mode = mode; element.SetBinding(property, binding); }
//public static void BindToControl(this LightweightViewModelBase vm, FrameworkElement control, DependencyProperty propertyTo, string propertyFrom, bool twoWay = false, IValueConverter converter = null, object converterParam = null) //{ // control.SetBinding(propertyTo, new Binding() { Source = vm, Path = new PropertyPath(propertyFrom), Converter = converter, ConverterParameter = converterParam, Mode = twoWay ? BindingMode.TwoWay : BindingMode.OneWay }); //} public static void BindToControl(this BaseViewModel vm, FrameworkElement control, DependencyProperty propertyTo, string propertyFrom, bool twoWay = false, IValueConverter converter = null, object converterParam = null) { control.SetBinding(propertyTo, new Binding() { Source = vm, Path = new PropertyPath(propertyFrom), Converter = converter, ConverterParameter = converterParam, Mode = twoWay ? BindingMode.TwoWay : BindingMode.OneWay }); }
protected void CreateBinding( DependencyProperty sourceDependencyProperty, string sourceDependencyPropertyName, FrameworkElement targetElement, DependencyProperty targetElementDependencyProperty) { var binding = ReadLocalValue(sourceDependencyProperty) as BindingExpression; if (binding != null) targetElement.SetBinding(targetElementDependencyProperty, binding.ParentBinding); else { targetElement.SetBinding(targetElementDependencyProperty, new Binding { Path = new PropertyPath(sourceDependencyPropertyName), Source = this }); } }
void SetAppBarForegroundBinding(FrameworkElement element) { element.SetBinding(Windows.UI.Xaml.Controls.Control.ForegroundProperty, new Windows.UI.Xaml.Data.Binding { Path = new PropertyPath("Control.ToolbarForeground"), Source = this, RelativeSource = new RelativeSource { Mode = RelativeSourceMode.TemplatedParent } }); }
/// <summary> /// Applies the Binding represented by the SetterValueBindingHelper. /// </summary> /// <param name="element">Element to apply the Binding to.</param> /// <param name="item">SetterValueBindingHelper representing the Binding.</param> private static void ApplyBinding(FrameworkElement element, SetterValueBindingHelper item) { if ((null == item.Property) || (null == item.Binding)) { 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; TypeInfo typeInfo = null; if (null == item.Type) { // No type specified; setting for the specified element type = element.GetType(); typeInfo = type.GetTypeInfo(); } else { // Try to get the type from the type system type = System.Type.GetType(item.Type); if (null == type) { // Search for the type in the list of assemblies foreach (var assembly in AssembliesToSearch) { // Match on short or full name typeInfo = assembly.DefinedTypes .Where(t => (t.FullName == item.Type) || (t.Name == item.Type)) .FirstOrDefault(); if (null != typeInfo) { // Found; done searching break; } } if (null == typeInfo) { // Unable to find the requested type anywhere throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Unable to access type \"{0}\". Try using an assembly qualified type name.", item.Type)); } } else { typeInfo = type.GetTypeInfo(); } } // Get the DependencyProperty for which to set the Binding DependencyProperty property = null; var field = typeInfo.GetDeclaredProperty(item.Property + "Property"); // type.GetRuntimeField(item.Property + "Property"); if (null != field) { property = field.GetValue(null) as DependencyProperty; } if (null == property) { // Unable to find the requsted property throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "Unable to access DependencyProperty \"{0}\" on type \"{1}\".", item.Property, type.Name)); } // Set the specified Binding on the specified property element.SetBinding(property, item.Binding); }
private void SetBinding(FrameworkElement obj, DependencyProperty p, string path) { Binding b = new Binding(); b.Source = this; b.Path = new PropertyPath(path); b.Mode = BindingMode.OneWay; obj.SetBinding(p, b); }
private void SetBinding(DependencyProperty dp, FrameworkElement element) { var binding = new Binding { Source = this, Path = new PropertyPath("Value"), Mode = BindingMode.TwoWay }; element.SetBinding(dp, binding); }
private static void WidthChanged(FrameworkElement element, IPropertyChangedArgs<string> args) { element.SetBinding(FrameworkElement.WidthProperty, args.NewValue); }
void SetAppBarForegroundBinding(FrameworkElement element) { element.SetBinding(Control.ForegroundProperty, new Windows.UI.Xaml.Data.Binding { Path = new PropertyPath("TitleBrush"), Source = _container, RelativeSource = new RelativeSource { Mode = RelativeSourceMode.TemplatedParent } }); }
public Task WaitForDataContext(FrameworkElement obj) { if(obj.DataContext != null) { this.Complete(obj.DataContext); } else { var b = new Binding(); var prop = DependencyProperty.RegisterAttached( "ListenAttachedDataContext" + binder.GetHashCode().ToString("{0:x}") + this.GetHashCode().ToString("{0:x}"), typeof(object), typeof(DataContextChangedDetector), new PropertyMetadata(null, new PropertyChangedCallback(onPropertyChanged))); obj.SetBinding(prop, b); } return tcs.Task; }