Click here to Skip to main content
15,886,776 members

Extraction and access of formatted text into/from a ResourceDictionary

Tastaturbeisser asked:

Open original thread
I am writing a multilanguage application. So each text for output (in C#-Code and in XAML too) has to be held in (Language-)ResourceDictionary. This works fine with plain text. E.g:

XML
<!-- The Dictionary for English language -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:System;assembly=mscorlib"
                    >
    <!-- Dialog 17-->
    <s:String x:Key="TextOfTbl7Key">This is the all plain text of TextBlock7 in English</s:String>
    ...
    ...
</ResourceDictionary>


XML
<!-- The application -->
<Window x:Class="MyAppl.Windows.Dialog17"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
         .....
         <TextBlock Text="{DynamicResource TextOfTbl7Key}"/>;
         ...
         ...
    </Grid>
</Window>


But what, if the TextBlock with text to extract contains formatted Text, i.e. the TextBlock has Inlines:

XML
<TextBlock Name="Tbl8">
   Normal stuff, something very <Bold>important</Bold>, and a <Italic>Name</Italic> of something. 
</TextBlock>


What I want is to export this as a single ... (whatever - but string does not work). No solution is to divide the text in many textparts (with own keys), because the composition of the text will depend on the language (And this solution would be ugly too). One solution could be to hold the entire TextBlock as a resource in the ResourceDictionary this way:

XML
<!-- The Dictionary for English language -->
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
                    xmlns:s="clr-namespace:System;assembly=mscorlib"
                    >
    <!-- Dialog 17-->
    <s:String x:Key="TextOfTbl7Key">This is boring text of TextBlock7 in English</s:String>
    <TextBlock x:Key="Tbl8Key">Normal stuff, something very <Bold>important</Bold>, and a <Italic>Name</Italic> of something </TextBlock>
    ...
    ...
</ResourceDictionary>




But I have no idea how to access the Resource-TextBlock as an Elemnet in XAML.

XML
<!-- The application -->
<Window x:Class="MyAppl.Windows.Dialog17"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Grid>
         .....
         <TextBlock Text="{DynamicResource TextOfTbl7Key}"/>;
         <!-- Here Tbl8 should be instantieted by access to ResourceDictionary --> 
         ...
         ...
    </Grid>
</Window>


Thanks for your answers in advance.
Tags: WPF, Globalization

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900