Obvio...">

WPF and string formatting

Suppose I have XAML:

<Window.Resources>
  <v:MyClass x:Key="whatever" Text="foo\nbar" />
</Window.Resources>

Obviously, I want a newline character in the MyClass.Text property, but the XAML parser builds an object with the literal string "foo \ nbar".

Is there (a) a way to convince the analyzer to translate the escape sequences, or (b) the .NET method to interpret the string in how the C # compiler will be?

I understand that I can find \nsequences there , but it would be better to have a general way to do this.

+3
source share
4 answers

You can use XML character escaping

<TextBlock Text="Hello&#13;World!"/>
+12
source

Try my head

  • Is a custom binding expression possible?

<v:MyClass x:Key="whatever" Text="{MyBinder foo\nbar}"/>

  1. Use static string resource?

  2. Text

<v:MyClass x:Key="whatever">
foo
bar
</v:MyClass>
+1

, \n , [...]

, , \n, - :

string s = "foo\\nbar";
s = s.Replace("\\n", "\n");

, b), , :

using System.Text.RegularExpressions;

// snip
string s = "foo\\nbar";
Regex r = new Regex("\\\\[rnt\\\\]");
s = r.Replace(s, ReplaceControlChars); ;
// /snip

string ReplaceControlChars(Match m)
{
    switch (m.ToString()[1])
    {
        case 'r': return "\r";
        case 'n': return "\n";
        case '\\': return "\\";
        case 't': return "\t";
        // some control character we don't know how to handle
        default: return m.ToString();
    }
}
+1

TextBlock . :

    <TextBlock>
        Line 1
        <LineBreak />
        Line 2
    </TextBlock>

- , .

0

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


All Articles