Onl*_*ere 97 c# wpf newline textblock
我正在从XML文件中提取文本,我想插入一些由textblock渲染为新行的新行.
我试过了:
<data>Foo bar baz \n baz bar</data>
Run Code Online (Sandbox Code Playgroud)
但是数据仍然显示没有新行.我设置的内容<data>
通过.Text
通过C#属性.
我需要在XML中放置什么才能在GUI中呈现新行?
我尝试过这样的手动设置XAML中的文本:
<TextBlock Margin="0 15 0 0" Width="600">
There
is a new line.
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
虽然编码字符没有出现在GUI中,但它也没有给我一个新的界限.
H.B*_*.B. 163
您可以尝试在数据中添加一个新行:
<data>Foo bar baz
baz bar</data>
Run Code Online (Sandbox Code Playgroud)
如果这不起作用,您可能需要手动解析字符串.
如果你需要直接的XAML,顺便说一下:
<TextBlock>
Lorem <LineBreak/>
Ipsum
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
Ket*_*omb 59
为了完整性:您也可以这样做:
<TextBlock Text="Line1
Line 2"/>
Run Code Online (Sandbox Code Playgroud)
小智 19
您也可以使用绑定
<TextBlock Text="{Binding MyText}"/>
Run Code Online (Sandbox Code Playgroud)
并设置MyText如下:
Public string MyText
{
get{return string.Format("My Text \n Your Text");}
}
Run Code Online (Sandbox Code Playgroud)
Gam*_*ist 18
你必须使用
< SomeObject xml:space="preserve" > once upon a time ...
this line will be below the first one < /SomeObject>
Run Code Online (Sandbox Code Playgroud)
或者如果您愿意:
<SomeObject xml:space="preserve" /> once upon a time... this line below < / SomeObject>
Run Code Online (Sandbox Code Playgroud)
注意:如果你们都使用&10并且你转到文本的下一行,那么你将有两个空行.
有关详细信息,请访问:http: //msdn.microsoft.com/en-us/library/ms788746.aspx
Dan*_*gel 12
虽然这是一个老问题,但我只是遇到了问题,并且从给定的答案中解决了它.也许它可能对其他人有所帮助.
我注意到即使我的XML文件看起来像:
<tag>
<subTag>content with newline.\r\nto display</subTag>
</tag>
Run Code Online (Sandbox Code Playgroud)
当它被读入我的C#代码时,字符串有双反斜杠.
\\r\\n
Run Code Online (Sandbox Code Playgroud)
为了解决这个问题,我写了一个ValueConverter去除额外的反斜杠.
public class XmlStringConverter : IValueConverter
{
public object Convert(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
string valueAsString = value as string;
if (string.IsNullOrEmpty(valueAsString))
{
return value;
}
valueAsString = valueAsString.Replace("\\r\\n", "\r\n");
return valueAsString;
}
public object ConvertBack(
object value,
Type targetType,
object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
如果一切都失败了,你也可以使用
"My text needs a line break here" + System.Environment.NewLine + " This should be a new line"
Run Code Online (Sandbox Code Playgroud)
小智 8
<TextBlock Margin="4" TextWrapping="Wrap" FontFamily="Verdana" FontSize="12">
<Run TextDecorations="StrikeThrough"> Run cannot contain inline</Run>
<Span FontSize="16"> Span can contain Run or Span or whatever
<LineBreak />
<Bold FontSize="22" FontFamily="Times New Roman" >Bold can contains
<Italic>Italic</Italic></Bold></Span>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
116838 次 |
最近记录: |