/// <summary> /// 提供数据绑定的核心方法。 /// </summary> /// <param name="context">绑定上下文</param> /// <param name="path">绑定路径</param> /// <param name="value">绑定值</param> /// <param name="nullBehavior">为空时执行的操作</param> private static void BindCore(this IHtmlElement element, string path, string value, BindingNullBehavior nullBehavior) { if (value == null) { switch (nullBehavior) { case BindingNullBehavior.Ignore: break; case BindingNullBehavior.Hidden: BindingContext.Action(element, e => e.Style().SetValue("visibility", "hidden")); break; case BindingNullBehavior.Remove: BindingContext.Action(element, e => e.Remove()); break; case BindingNullBehavior.DisplayNone: BindingContext.Action(element, e => e.Style().SetValue("display", "none")); break; default: throw new NotSupportedException(); } return; } var attributeMatch = attributePathRegex.Match(path); if (attributeMatch.Success) { string attributeName = attributeMatch.Groups["name"].Value; BindingContext.Action(element, e => element.SetAttribute(attributeName, value)); } if (path == "@:text") { BindingContext.Action(element, e => e.InnerText(value)); } if (path == "@:html") { BindingContext.Action(element, e => e.InnerHtml(value)); } return; }
public static IHtmlElement Bind(this IHtmlElement element, BindingContext context, string path, object value, string format, BindingNullBehavior nullBehavior) { if (format == null) { format = "{0}"; } string _value = null; if (value != null) { _value = string.Format(format, value); } BindCore(element, path, _value, nullBehavior); return(element); }
/// <summary> /// 绑定数据到指定位置 /// </summary> /// <param name="element">要绑定数据的元素</param> /// <param name="path">绑定路径</param> /// <param name="value">绑定值</param> /// <param name="format">用于绑定值的格式化字符串</param> /// <param name="nullBehavior">定义当绑定值为空的行为</param> /// <returns>被绑定的元素</returns> public static IHtmlElement Bind(this IHtmlElement element, string path, object value, string format, BindingNullBehavior nullBehavior) { return(Bind(element, BindingContext.Current, path, value, format, nullBehavior)); }
/// <summary> /// 绑定数据到指定位置,集合版本 /// </summary> /// <param name="elements">要绑定数据的元素</param> /// <param name="path">绑定路径</param> /// <param name="value">绑定值</param> /// <param name="format">用于绑定值的格式化字符串</param> /// <param name="nullBehavior">定义当绑定值为空的行为</param> /// <returns>被绑定的元素</returns> public static IEnumerable <IHtmlElement> Bind(this IEnumerable <IHtmlElement> elements, string path, object value, string format, BindingNullBehavior nullBehavior) { return(elements.ForAll(e => e.Bind(path, value, format, nullBehavior))); }
/// <summary> /// 绑定数据到指定位置 /// </summary> /// <param name="element">要绑定数据的元素</param> /// <param name="path">绑定路径</param> /// <param name="value">绑定值</param> /// <param name="nullBehavior">定义当绑定值为空的行为</param> /// <returns>被绑定的元素</returns> public static IHtmlElement Bind(this IHtmlElement element, string path, object value, BindingNullBehavior nullBehavior) { return(Bind(element, path, value, null, nullBehavior)); }