static void InnerHTML_binding_With_longPropertyPath(Assert Assert) { var model = new Model { UserInfo = new UserInfoContract { StringProperty0 = "Test1" } }; var div = DOM.CreateElement("div"); var htmlBindingInfo = new HTMLBindingInfo { Source = model, SourcePath = "UserInfo.StringProperty0", Target = div, TargetPath = "innerHTML", BindingMode = BindingMode.OneWay }; htmlBindingInfo.Connect(); model.UserInfo.StringProperty0 = "A"; Assert.AreEqual("A", div.Html()); div = TestHelper.BuildAndGetFirstLogicalChild("<div>{UserInfo.StringProperty0}</div>", model).Root; Assert.AreEqual("A", div.Html()); model.UserInfo.StringProperty0 = "B"; Assert.AreEqual("B", div.Html()); }
static void ShouldSupportTypeWhenSourcePropertyTypeIsObjectAnd_Target_Is_HTML_Element(Assert assert) { var source = new A(); var target = new jQuery(Document.CreateElement("input")); target.Attr("type", "hidden"); var bindingInfo = new HTMLBindingInfo { BindingMode = BindingMode.TwoWay, SourcePath = nameof(source.ObjectProperty), Source = source, Target = target, TargetPath = "value" }; bindingInfo.Connect(); var value = 56; source.ObjectProperty = value; assert.AreEqual("56", target.Val()); target.Val("76"); target.Trigger(EventType.Change); assert.AreEqual(typeof(int).FullName, source.ObjectProperty.GetType().FullName); assert.AreEqual("76", source.ObjectProperty.ToString()); }
object BuildTextNode(Element xmlNode, FrameworkElement parentInstance) { // skip empty spaces var html = xmlNode.GetInnerText(); if (string.IsNullOrWhiteSpace(html)) { return(null); } // maybe <div> {LastName} </div> var bindingInfo = HTMLBindingInfo.TryParseExpression(html); if (bindingInfo != null) { var textNode = new jQuery(Document.CreateTextNode("")); parentInstance._root.Append(textNode); bindingInfo.BindingMode = BindingMode.OneWay; bindingInfo.Source = parentInstance; bindingInfo.SourcePath = "DataContext." + bindingInfo.SourcePath.Path; bindingInfo.Target = textNode; bindingInfo.TargetPath = nameof(parentInstance.InnerHTML); bindingInfo.Connect(); return(null); } var instanceAsContentControl = parentInstance as ContentControl; if (instanceAsContentControl != null) { instanceAsContentControl.Content = html; return(null); } parentInstance._root.Append(html); return(null); }
static void simpleBindingfor_src(Assert Assert) { var simpleClass1 = new SimpleClass1(); var img = DOM.CreateElement("img"); var htmlBindingInfo = new HTMLBindingInfo { Source = simpleClass1, SourcePath = "LastName", Target = img, TargetPath = "src", BindingMode = BindingMode.OneWay }; htmlBindingInfo.Connect(); simpleClass1.LastName = TestHelper.SampleImageUrl_350_150; Assert.AreEqual(TestHelper.SampleImageUrl_350_150, img.Attr("src")); }
void ProcessAttribute(object instance, string name, string value) { var nameUpperCase = name.ToUpperCase(); if (nameUpperCase == "DATACONTEXT" || name == AttributeName_d_designerdataContext) { return; } if (name == "class") { name = "Class"; } var targetProperty = ReflectionHelper.FindProperty(instance, name, FindPropertyFlag); if (targetProperty != null && name != targetProperty.Name) { name = targetProperty.Name; } var bi = BindingInfo.TryParseExpression(value); if (bi != null) { var eventInfo = ReflectionHelper.FindEvent(instance, name, FindPropertyFlag); if (eventInfo != null) { var methodInfo = ReflectionHelper.GetMethodInfo(DataContext, bi.SourcePath.Path); var handler = Delegate.CreateDelegate(eventInfo.AddMethod.ParameterTypes.First(), DataContext, methodInfo); eventInfo.AddEventHandler(instance, handler); return; } if (name.Contains(".") == false) { if (targetProperty == null) { var htmlBindingInfo = new HTMLBindingInfo { Source = instance, SourcePath = new PropertyPath("DataContext." + bi.SourcePath.Path), Target = instance.As <FrameworkElement>()._root, TargetPath = name, BindingMode = BindingMode.OneWay }; if (bi.BindingMode == BindingMode.TwoWay && HTMLBindingInfo.TargetCanUpdateSource(htmlBindingInfo.Target)) { htmlBindingInfo.BindingMode = BindingMode.TwoWay; } htmlBindingInfo.Connect(); return; } } bi.SourcePath = new PropertyPath("DataContext." + bi.SourcePath.Path); bi.Source = instance; bi.Target = instance; bi.TargetPath = name; bi.Connect(); return; } if (targetProperty != null) { if (targetProperty.PropertyType.IsEnum) { ReflectionHelper.SetPropertyValue(instance, name, Enum.Parse(targetProperty.PropertyType, value, true)); return; } var converterAttributes = targetProperty.GetCustomAttributes(typeof(TypeConverterAttribute)); var firstConverterAtribute = converterAttributes?.FirstOrDefault(); if (firstConverterAtribute != null) { var converter = (TypeConverterAttribute)firstConverterAtribute; var valueConverter = (IValueConverter)Activator.CreateInstance(converter._type); var convertedValue = valueConverter.Convert(value, targetProperty.PropertyType, null, CultureInfo.CurrentCulture); ReflectionHelper.SetPropertyValue(instance, targetProperty.Name, convertedValue); return; } var propertyValue = Cast.To(value, targetProperty.PropertyType, CultureInfo.CurrentCulture); ReflectionHelper.SetPropertyValue(instance, targetProperty.Name, propertyValue); return; } if (name.StartsWith("on.")) { var eventName = name.RemoveFromStart("on."); // support this format: this.Notify(OnContactClicked) if (value.StartsWith("this.")) { var viewInvocationExpressionInfo = ViewInvocationExpressionInfo.Parse(value); var methodName = viewInvocationExpressionInfo.MethodName; var mi = Caller.GetType().GetMethod(methodName, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); instance.As <FrameworkElement>().On(eventName, () => { if (mi == null) { throw new MissingMemberException(Caller.GetType().FullName + "->" + methodName); } mi.Invoke(Caller, viewInvocationExpressionInfo.Parameters.ToArray()); }); return; } var methodInfo = Caller.GetType().GetMethod(value, ReflectionHelper.AllBindings); instance.As <FrameworkElement>().On(eventName, () => { if (methodInfo == null) { throw new MissingMemberException(Caller.GetType().FullName + "::" + value); } methodInfo.Invoke(Caller); }); return; } if (nameUpperCase.StartsWith("CSS.")) { var styleAttributeName = name.Substring(4); instance.As <FrameworkElement>()._root.Css(styleAttributeName, value); return; } if (nameUpperCase == "X.NAME" || nameUpperCase == "X:NAME") { ReflectionHelper.SetNonStaticField(Caller, value, instance); return; } instance.As <FrameworkElement>()._root.Attr(name, value); }