Task: serialize the contents of TextBlock 'a (to preserve the original content), change and substitute it in TextBlock .
With serialization, trouble is simple. Found 2 ways, but both than something else does not fit.
Method 1:
Inlines go through the Inlines collection of the Inlines block and with the help of XamlWriter.Save(inline) save all the elements. Then I restore using XamlReader.Parse(string) .
The downside is that XamlWriter is able to save only those properties that were assigned in runtime, so if I fill TextBlock in the designer, all elements (Run, Span, etc.) are saved without properties and even without text. Just empty.
Method 2:
// Текущим элементом тут является `TextBlock` // _content имеет тип string var tr = new TextRange(ContentStart, ContentEnd); using(var stream = new MemoryStream()) { tr.Save(stream, DataFormats.Xaml); stream.Position = 0; using(var reader = new StreamReader(stream)) { _content = reader.ReadToEnd(); } } In the text block I have this
<Run Text="{Binding Property}" FontWeight="Bold"/> _content written in _content
<Span xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xml:space="preserve" xml:lang="en-us" FlowDirection="LeftToRight" NumberSubstitution.CultureSource="Text" NumberSubstitution.Substitution="AsCulture" FontFamily="Segoe UI" FontStyle="Normal" FontWeight="Bold" FontStretch="Normal" FontSize="12" Foreground="#FF000000" Typography.StandardLigatures="True" Typography.ContextualLigatures="True" Typography.DiscretionaryLigatures="False" Typography.HistoricalLigatures="False" Typography.AnnotationAlternates="0" Typography.ContextualAlternates="True" Typography.HistoricalForms="False" Typography.Kerning="True" Typography.CapitalSpacing="False" Typography.CaseSensitiveForms="False" Typography.StylisticSet1="False" Typography.StylisticSet2="False" Typography.StylisticSet3="False" Typography.StylisticSet4="False" Typography.StylisticSet5="False" Typography.StylisticSet6="False" Typography.StylisticSet7="False" Typography.StylisticSet8="False" Typography.StylisticSet9="False" Typography.StylisticSet10="False" Typography.StylisticSet11="False" Typography.StylisticSet12="False" Typography.StylisticSet13="False" Typography.StylisticSet14="False" Typography.StylisticSet15="False" Typography.StylisticSet16="False" Typography.StylisticSet17="False" Typography.StylisticSet18="False" Typography.StylisticSet19="False" Typography.StylisticSet20="False" Typography.Fraction="Normal" Typography.SlashedZero="False" Typography.MathematicalGreek="False" Typography.EastAsianExpertForms="False" Typography.Variants="Normal" Typography.Capitals="Normal" Typography.NumeralStyle="Normal" Typography.NumeralAlignment="Normal" Typography.EastAsianWidths="Normal" Typography.EastAsianLanguage="Normal" Typography.StandardSwashes="0" Typography.ContextualSwashes="0" Typography.StylisticAlternates="0" BaselineAlignment="Baseline" TextDecorations=""><Run>qwe</Run></Span> What I did was keep all the formatting for the current element from the parent and at the same time transferred FontWeight="Bold" to the overall style. That is, in this case, I have 2 options:
- Trim common styles and lose specific formatting with them.
- Leave everything as it is, but it will pop up, if at runtime someone changes some style in the text block.
I do not know what to do. Option 2.2 is the most suitable, but still a curve. The execution time of the code also matters, therefore, "not to run through all the properties of the original and include / exclude in the result".