/// <summary> /// 设置文本大小写 /// </summary> /// <param name="s">原始文本</param> /// <param name="textCase">大小写</param> /// <returns></returns> public static string ApplyTextCase(string s, TextCases textCase) { if (string.IsNullOrWhiteSpace(s)) { return(s); } if (textCase == TextCases.Orginal) { return(s); // 使用原始大小写 } if (textCase == TextCases.Upper) { return(s.ToUpperInvariant()); // 使用全大写 } if (textCase == TextCases.Lower) { return(s.ToLowerInvariant()); // 使用全小写 } if (textCase == TextCases.Title) { return(CultureInfo.InvariantCulture.TextInfo.ToTitleCase(s)); // 单词首字母大写 } return(s); }
private void RefreshText() { // 为了增加效率,Loaded之后才刷新,而且这样才能避免初始化显示时大小写失效的BUG if (!this.IsLoaded) { return; } string originalText = this.OriginalText; TextCases textCase = this.TextCase; if (!string.IsNullOrEmpty(originalText)) { this.Text = TextHelper.ApplyTextCase(originalText, textCase); return; } object[] parameters = new[] { Parameter0, Parameter1, Parameter2, Parameter3, Parameter4, Parameter5, Parameter6, Parameter7, Parameter8, Parameter9 }; string formatText = LanguageHelper.GetLanguage(this.TextKey); if (string.IsNullOrEmpty(formatText)) { this.Text = null; return; } StringBuilder sb = new StringBuilder(); TextHelper.AnalyzeFormatString(formatText, (isParam, paramIndex, text) => { if (isParam) { string param = null; if (parameters.Length > paramIndex && parameters[paramIndex] != null) { param = parameters[paramIndex].ToString(); } else { param = string.Format("{{{0}}}", paramIndex); } sb.Append(param); } else { sb.Append(text); } }); this.Text = TextHelper.ApplyTextCase(sb.ToString(), textCase); }
public virtual void InvalidTestCase(TextCases oldValue, TextCases newValue) { RefreshText(); }
public static void SetTextCase(DependencyObject dObj, TextCases value) { dObj.SetValue(TextCaseProperty, value); }
private void RefreshText() { if (this.IsUseTextBlockMode) { return; } // 为了在属性IsUseTextBlockMode被初始化之前不执行RefreshText,所以在Loaded之后 // 才组合文本 if (!this.IsLoaded) { return; } string originalText = this.OriginalText; TextCases textCase = this.TextCase; var inlines = this.Inlines; if (inlines.Count > 0) { inlines.Clear(); } if (!string.IsNullOrEmpty(originalText)) { inlines.Add(new TKRun { OriginalText = originalText }); return; } object[] parameters = new[] { Parameter0, Parameter1, Parameter2, Parameter3, Parameter4, Parameter5, Parameter6, Parameter7, Parameter8, Parameter9 }; string formatText = LanguageHelper.GetLanguage(this.TextKey); if (string.IsNullOrEmpty(formatText)) { return; } TextHelper.AnalyzeFormatString(formatText, (isParam, paramIndex, text) => { if (isParam) { if (parameters.Length > paramIndex && parameters[paramIndex] != null) { Inline param = null; if (parameters[paramIndex] is Inline) { param = parameters[paramIndex] as Inline; } else { param = new TKRun { OriginalText = parameters[paramIndex].ToString() } }; inlines.Add(param); } else { inlines.Add(new TKRun { OriginalText = string.Format("{{{0}}}", paramIndex) }); } } else { inlines.Add(new TKRun { OriginalText = text }); } }); }
public virtual void InvalidTestCase(TextCases oldValue, TextCases newValue) { }