private Type GetArgType(XamlBinding binding) { object context; if (string.IsNullOrEmpty(binding.ElementName)) { context = _source.DataContext; } else { #if AVALONIA var root = GetRootParent(_source) as IControl; context = root.FindControl <IControl>(binding.ElementName); #else var root = System.Windows.Window.GetWindow(_source) ?? (XamlControl)GetRootParent(_source); context = root.FindName(binding.ElementName) as System.Windows.DependencyObject; #endif } #if AVALONIA var path = binding.Path; #else var path = binding.Path?.Path; #endif return(string.IsNullOrEmpty(path) || path == "." ? context.GetType() : GetArgType(context, path)); }
public void AttachPropertyChangedHandler_Should_Be_Called_On_Chain() { var source = new Mock<IObservableDependencyObject>(); var foo = new Mock<IObservableDependencyObject>(); var bar = new Mock<IObservableDependencyObject>(); Mock<IPropertyPathParser> mockPathParser = new Mock<IPropertyPathParser>(); mockPathParser.Setup(x => x.Parse(source.Object, "Foo.Bar")).Returns(new[] { new PropertyPathToken(source.Object, "Foo"), new PropertyPathToken(foo.Object, "Bar"), new PropertyPathToken(bar.Object, null), }); Mock<DependencyObject> mockTarget = new Mock<DependencyObject>(); Binding binding = new Binding { Path = new PropertyPath("Foo.Bar"), Source = source.Object, }; BindingExpression target = new BindingExpression( mockPathParser.Object, mockTarget.Object, Control.BackgroundProperty, binding); target.GetValue(); source.Verify(x => x.AttachPropertyChangedHandler("Foo", It.IsAny<DependencyPropertyChangedEventHandler>())); foo.Verify(x => x.AttachPropertyChangedHandler("Bar", It.IsAny<DependencyPropertyChangedEventHandler>())); bar.Verify(x => x.AttachPropertyChangedHandler("Bar", It.IsAny<DependencyPropertyChangedEventHandler>()), Times.Never()); }
public BindingExpression( IPropertyPathParser pathParser, DependencyObject target, DependencyProperty dp, Binding binding) : base(target, dp) { this.pathParser = pathParser; this.ParentBinding = binding; }
internal static object ResolveBinding(XamlBinding binding, XamlControl source, int position) { #if AVALONIA var instancedBinding = binding.Initiate(source, _properties[position]); BindingOperations.Apply(source, _properties[position], instancedBinding, null); #else if (!System.Windows.Data.BindingOperations.IsDataBound(source, _properties[position])) { System.Windows.Data.BindingOperations.SetBinding(source, _properties[position], binding); } #endif return(source.GetValue(_properties[position])); }
private Type HandleBindingArg(XamlBinding binding, ICollection <Action <ILGenerator> > opCodes, int position) { var argType = GetArgType(binding); opCodes.Add(b => b.Emit(OpCodes.Ldarg_0)); opCodes.Add(b => b.Emit(OpCodes.Ldfld, _instanceField)); opCodes.Add(b => b.Emit(OpCodes.Ldc_I4, position)); var resolveBindingMethod = GetType().GetMethod(nameof(ResolveBinding), BindingFlags.NonPublic | BindingFlags.Static); opCodes.Add(b => b.Emit(OpCodes.Call, resolveBindingMethod)); if (argType != typeof(object)) { opCodes.Add(b => b.Emit(argType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass, argType)); } return(argType); }
public override object ProvideValue(System.IServiceProvider serviceProvider) { Binding result = new Binding(this.path); result.RelativeSource = new RelativeSource(RelativeSourceMode.TemplatedParent); return result; }
public BindingExpression SetBinding(DependencyProperty dp, Binding binding) { PropertyPathParser pathParser = new PropertyPathParser(); BindingExpression expression = new BindingExpression(pathParser, this, dp, binding); object oldValue = this.GetValue(dp); object newValue = expression.GetValue(); this.propertyBindings.Add(dp, expression); this.SetValueInternal(dp, oldValue, newValue); return expression; }