public string ToInlineCSS(RichTextBoxStyle currentStyle, UIElement element) { /*StringBuilder result = new StringBuilder(); * Dictionary<string, string> pairs = ToHTML(); * Dictionary<string, string> current = currentStyle.ToHTML(); * string value; * * foreach (KeyValuePair<string, string> pair in pairs) * { * if (current.TryGetValue(pair.Key, out value)) * { * if (value == pair.Value) * { * continue; * } * } * result.Append(pair.Key + ":" + pair.Value + ";"); * } * * return result.ToString();*/ StringBuilder result = new StringBuilder(); Dictionary <string, string> pairs; Dictionary <string, string> current; string value; pairs = ToHTML(element); current = currentStyle.ToHTML(element); foreach (KeyValuePair <string, string> pair in pairs) { if (current.TryGetValue(pair.Key, out value)) { current.Remove(pair.Key); if (value == pair.Value) { continue; } } result.Append(pair.Key + ":" + pair.Value + ";"); } pairs = GetStyleDefaults(current); foreach (KeyValuePair <string, string> pair in pairs) { result.Append(pair.Key + ":" + pair.Value + ";"); } return(result.ToString()); }
/// <summary> /// Applies the provided style to the bullet /// </summary> /// <param name="style">RichTextBoxStyle object</param> public void ApplyStyle(RichTextBoxStyle style) { FontFamily = new FontFamily(style.Family); FontSize = (style.Size != null ? (double)style.Size : 14); Foreground = style.Foreground; FontWeight = (style.Weight != null ? (FontWeight)style.Weight : FontWeights.Normal); FontStyle = (style.Style != null ? (FontStyle)style.Style : FontStyles.Normal); HorizontalAlignment = style.Alignment; VerticalAlignment = VerticalAlignment.Center; if (ElementContent != null && ElementContent.Content is TextBlock) { TextBlock tb = (TextBlock)ElementContent.Content; tb.FontFamily = FontFamily; tb.FontSize = FontSize; tb.Foreground = Foreground; tb.FontWeight = FontWeight; tb.FontStyle = FontStyle; } UpdateVisualState(); }
/// <summary> /// Sets the specified property to a value /// </summary> /// <param name="key">Property name</param> /// <param name="value">Property value</param> /// <param name="xaml">Indicates whether the source value is XAML</param> public void SetProperty(string key, string value, bool xaml) { Color tempColor; if (value != null) { switch (key) { case "ID": ID = value; break; case "FontFamily": Family = value; break; case "FontSize": Size = double.Parse(value, CultureInfo.InvariantCulture); break; case "FontStyle": switch (value.ToLower()) { case "italic": Style = FontStyles.Italic; break; default: Style = FontStyles.Normal; break; } break; case "FontWeight": switch (value.ToLower()) { case "bold": Weight = FontWeights.Bold; break; default: Weight = FontWeights.Normal; break; } break; case "Decoration": switch (value.ToLower()) { case "underline": Decorations = TextDecorations.Underline; break; case "line-through": Effect = TextBlockPlusEffect.Strike; break; default: Decorations = null; break; } break; case "Alignment": try { Alignment = (HorizontalAlignment)Enum.Parse(typeof(HorizontalAlignment), value, true); } catch (Exception ex) { Alignment = HorizontalAlignment.Left; } break; case "Background": tempColor = RichTextBoxStyle.StringToColor(value.TrimStart('#')); Background = new SolidColorBrush(tempColor); break; case "Foreground": tempColor = RichTextBoxStyle.StringToColor(value.TrimStart('#')); Foreground = new SolidColorBrush(tempColor); break; case "BorderBrush": tempColor = RichTextBoxStyle.StringToColor(value.TrimStart('#')); BorderBrush = new SolidColorBrush(tempColor); break; case "ShadowBrush": tempColor = RichTextBoxStyle.StringToColor(value.TrimStart('#')); ShadowBrush = new SolidColorBrush(tempColor); break; case "Special": Special = (RichTextSpecialFormatting)Enum.Parse(typeof(RichTextSpecialFormatting), value, true); break; case "Effect": Effect = (TextBlockPlusEffect)Enum.Parse(typeof(TextBlockPlusEffect), value, true); break; case "Border": BorderType = (BorderEffect)Enum.Parse(typeof(BorderEffect), value, true); break; case "Shadow": Shadow = (ShadowEffect)Enum.Parse(typeof(ShadowEffect), value, true); break; case "Margin": Margin = Utility.ParseThickness(value, xaml); break; case "VerticalAlignment": VerticalAlignment = (VerticalAlignment)Enum.Parse(typeof(VerticalAlignment), Regex.Replace(value, "middle", "Center", RegexOptions.IgnoreCase), true); break; } } }
public RichTextBoxStyle(RichTextBoxStyle style) : this(style.ID, style.Family, style.Size, style.Background, style.Foreground, style.BorderBrush, style.ShadowBrush, style.Weight, style.Style, style.Decorations, style.Alignment, style.Special, style.Effect, style.BorderType, style.Shadow, new Thickness(style.Margin.Left, style.Margin.Top, style.Margin.Right, style.Margin.Bottom), style.VerticalAlignment) { }
public void ApplyStyle(RichTextBoxStyle style, RichTextBoxStyle linkStyle, bool useLinkButtons) { RichTextTag tag = new RichTextTag(); bool isLink = false; this.IsHitTestVisible = false; if (this.Tag is RichTextTag) { tag = (RichTextTag)this.Tag; if (tag.Metadata != null) { isLink = tag.Metadata.IsLink; } if (useLinkButtons) { if (isLink) { if (_link == null) { Children.Remove(_element); _element.Foreground = new SolidColorBrush(Colors.Red); _link = new HyperlinkButton() { Content = _element, NavigateUri = new Uri(tag.Metadata["URL"]), TargetName = (tag.Metadata.ContainsKey("Target") ? tag.Metadata["Target"] : "_blank") }; Children.Insert(0, _link); } this.IsHitTestVisible = true; } else { if (_link != null) { Children.Remove(_link); _link = null; Children.Insert(0, _element); } } } } _element.FontFamily = new FontFamily(style.Family); _element.FontSize = (style.Size != null ? (double)style.Size : 14); base.Background = style.Background; _element.Foreground = isLink ? linkStyle.Foreground : style.Foreground; _element.FontWeight = (style.Weight != null ? (FontWeight)style.Weight : FontWeights.Normal); _element.FontStyle = (style.Style != null ? (FontStyle)style.Style : FontStyles.Normal); _element.TextDecorations = isLink ? linkStyle.Decorations : style.Decorations; _effect = style.Effect; _border = style.BorderType; _shadowEffect = style.Shadow; HorizontalAlignment = style.Alignment; VerticalAlignment = style.VerticalAlignment; if (style.ShadowBrush != null) { _shadowBrush = style.ShadowBrush; } if (style.Special == RichTextSpecialFormatting.Subscript) { _element.FontSize *= RichTextBlock.SubScriptMultiplier; VerticalAlignment = VerticalAlignment.Bottom; LineHeightMultiplier = 1 / RichTextBlock.SubScriptMultiplier; } else if (style.Special == RichTextSpecialFormatting.Superscript) { _element.FontSize *= RichTextBlock.SuperScriptMultiplier; VerticalAlignment = VerticalAlignment.Top; LineHeightMultiplier = 1 / RichTextBlock.SuperScriptMultiplier; } else { LineHeightMultiplier = 1; } Margin = new Thickness(style.Margin.Left, style.Margin.Top, style.Margin.Right, style.Margin.Bottom); UpdateStyle(); }