Can I dynamically add part of TextBlock.Text to a different color?

I have a TextBlock in my main form. I set the Text property for different lines during application startup.

I would like to be able to color parts of specific strings.

Pseudocode:

if(a < 0) txbStatus.Text = string.Format("{0} <RED>{1}</RED>", a, b); else txbStatus.Text = string.Format("{0} <BLUE>{1}</RED>", a, b); 
+4
source share
3 answers

You can split your line however you want, and then use the foreach() for this line split try

 TextBlockName.Inlines.Add(new Run("colored text") {Foreground = Brushes.Blue}); 
+11
source

The content of the TextBox should not be just a string, but a set of Inline s:

 txbStatus.Inlines.Clear(); txbStatus.Inlines.Add(new Run("normal color, ")); txbStatus.Inlines.Add(new Run("colored text") { Foreground = Brushes.Red }); 
+5
source

I created my own text block that will help you highlight a piece of text in the text value of a TextBlock.

 using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; namespace UI.WPF.UserControls { class CustomTextBlock:TextBlock { string _originalText; public string HighlighText { get { return (string)GetValue(HighlighTextProperty); } set { SetValue(HighlighTextProperty, value); RenderHighlightedText(); } } // Using a DependencyProperty as the backing store for HighlighText. This enables animation, styling, binding, etc... public static readonly DependencyProperty HighlighTextProperty = DependencyProperty.Register("HighlighText", typeof(string), typeof(CustomTextBlock), new FrameworkPropertyMetadata(new PropertyChangedCallback(HighlighTextProperty_Changed)) ); private static void HighlighTextProperty_Changed(DependencyObject d, DependencyPropertyChangedEventArgs e) { CustomTextBlock textBlock = (CustomTextBlock)d; textBlock.RenderHighlightedText(); } public CustomTextBlock() : base() { } static CustomTextBlock() { DefaultStyleKeyProperty.OverrideMetadata( typeof(CustomTextBlock), new FrameworkPropertyMetadata(typeof(CustomTextBlock))); } public override void OnApplyTemplate() { base.OnApplyTemplate(); } protected override void OnInitialized(EventArgs e) { base.OnInitialized(e); _originalText = Text; RenderHighlightedText(); } private Run GetFormatedText(string text, bool isBold) { Run noramlRun = new Run(text); if (isBold) { noramlRun.FontWeight = FontWeights.Bold; } else { noramlRun.FontWeight = FontWeights.Normal; } return noramlRun; } public void RenderHighlightedText() { var boldText = HighlighText; if (!string.IsNullOrEmpty(HighlighText) && _originalText.ToLower().Contains(boldText.ToLower())) { this.Inlines.Clear(); int point = _originalText.ToLower().IndexOf(boldText.ToLower()); string strHighlighted = _originalText.Substring(point, HighlighText.Length); Run runHighlight = GetFormatedText(strHighlighted, true); if (point == 0) { this.Inlines.Add(runHighlight); int remainingLength = _originalText.Length - (point + HighlighText.Length); string remaingText = _originalText.Substring((point + HighlighText.Length), remainingLength); this.Inlines.Add(GetFormatedText(remaingText, false)); } else { string firstPart = _originalText.Substring(0, point); this.Inlines.Add(GetFormatedText(firstPart, false)); this.Inlines.Add(runHighlight); int remainingLength = _originalText.Length - (point + HighlighText.Length); string remaingText = _originalText.Substring((point + HighlighText.Length), remainingLength); this.Inlines.Add(GetFormatedText(remaingText, false)); } } else { this.Inlines.Clear(); this.Inlines.Add(GetFormatedText(_originalText, false)); } } } } 

Method of use.

  <usercontrol:CustomTextBlock Text="{Binding Title}" HighlighText="{Binding DataContext.SearchText, Mode=TwoWay, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type Page}}}" /> 

For more information

https://sites.google.com/site/greateindiaclub/mobil-apps/windows8/highlightpartoftextinwpftextblockcontrol

+3
source

Source: https://habr.com/ru/post/1310653/


All Articles