/// <summary> /// 设置多个元素的样式 /// </summary> /// <param name="newStyle">新样式</param> /// <param name="document">文档对象</param> /// <param name="elements">要设置的元素列表</param> /// <returns>操作是否成功</returns> internal static bool SetElementStyle( DocumentContentStyle newStyle, DomDocument document, System.Collections.IEnumerable elements) { if (newStyle == null) { throw new ArgumentNullException("newStyle"); } if (document == null) { throw new ArgumentNullException("document"); } if (elements == null) { throw new ArgumentNullException("elements"); } if (document.Options.SecurityOptions.EnablePermission) { // 运行授权控制,向样式信息添加用户信息 newStyle.DisableDefaultValue = false; newStyle.CreatorIndex = document.UserHistories.CurrentIndex; newStyle.DeleterIndex = -1; } else { // 去除授权控制相关信息 newStyle.DisableDefaultValue = false; newStyle.CreatorIndex = -1; newStyle.DeleterIndex = -1; } Dictionary <DomElement, int> newStyleIndexs = new Dictionary <DomElement, int>(); DomElementList parents = new DomElementList(); DomElement lastParent = null; foreach (DomElement element in elements) { DomElement parent = element.Parent; //if (parent != lastParent) { // 记录所有涉及到的父元素 lastParent = parent; bool addParent = false; //if (element is XTextFieldBorderElement) //{ // addParent = true; //} //else if (addParent) { if (parents.Contains(element.Parent) == false) { parents.Add(element.Parent); } } }//if DocumentContentStyle rs = (DocumentContentStyle)element.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { rs.DefaultValuePropertyNames = newStyle.GetDefaultValuePropertyNames(); int styleIndex = document.ContentStyles.GetStyleIndex(rs); if (styleIndex != element.StyleIndex) { newStyleIndexs[element] = styleIndex; } if (element.ShadowElement != null && styleIndex != element.ShadowElement.StyleIndex) { newStyleIndexs[element.ShadowElement] = styleIndex; } } }//foreach if (parents.Count > 0) { // 对涉及到的父元素设置样式 foreach (DomElement element in parents) { DocumentContentStyle rs = (DocumentContentStyle)element.RuntimeStyle.Clone(); if (XDependencyObject.MergeValues(newStyle, rs, true) > 0) { rs.DefaultValuePropertyNames = newStyle.GetDefaultValuePropertyNames(); int styleIndex = document.ContentStyles.GetStyleIndex(rs); if (styleIndex != element.StyleIndex) { newStyleIndexs[element] = styleIndex; } } } } if (newStyleIndexs.Count > 0) { DomElementList result = document.EditorSetElementStyle(newStyleIndexs, true); if (result != null && result.Count > 0) { return(true); } } return(false); }