当文本被设置为属性时,我如何添加换行符:
<TextBlock Text="Stuff on line1 \n Stuff on line2" />
把它分解成分解格式不适合我的特殊情况。我需要的是一些方法来模仿以下:
<TextBlock>
<TextBlock.Text>
Stuff on line1 <LineBreak/>
Stuff on line2
</TextBlock.Text>
<TextBlock/>
当文本被设置为属性时,我如何添加换行符:
<TextBlock Text="Stuff on line1 \n Stuff on line2" />
把它分解成分解格式不适合我的特殊情况。我需要的是一些方法来模仿以下:
<TextBlock>
<TextBlock.Text>
Stuff on line1 <LineBreak/>
Stuff on line2
</TextBlock.Text>
<TextBlock/>
当前回答
对于那些已经尝试了这个问题的所有答案,但仍在挠头为什么它们都不适合你的人来说,你可能遇到了我遇到的某种形式的问题。
我的TextBlock。Text属性位于ToolTipService内部。元素,它绑定到一个对象的属性,该对象的数据正在从SQL存储过程中提取。现在,存储过程中这个特定属性的数据正在从一个SQL函数中提取。
因为没有什么对我有用,我放弃了我的搜索,并创建了下面的转换器类:
public class NewLineConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var s = string.Empty;
if (value.IsNotNull())
{
s = value.ToString();
if (s.Contains("\\r\\n"))
s = s.Replace("\\r\\n", Environment.NewLine);
if (s.Contains("\\n"))
s = s.Replace("\\n", Environment.NewLine);
if (s.Contains("

"))
s = s.Replace("

", Environment.NewLine);
if (s.Contains("
"))
s = s.Replace("
", Environment.NewLine);
if (s.Contains("
"))
s = s.Replace("
", Environment.NewLine);
if (s.Contains(" "))
s = s.Replace(" ", Environment.NewLine);
if (s.Contains(" "))
s = s.Replace(" ", Environment.NewLine);
if (s.Contains(" "))
s = s.Replace(" ", Environment.NewLine);
if (s.Contains("<br />"))
s = s.Replace("<br />", Environment.NewLine);
if (s.Contains("<LineBreak />"))
s = s.Replace("<LineBreak />", Environment.NewLine);
}
return s;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
最后我不得不使用环境。NewLine方法来自@dparker的答案。我指示转换器寻找任何可能的换行符文本表示,并将其替换为Environment.NewLine。
这个工作!
然而,我仍然感到困惑,为什么没有其他方法适用于绑定属性。
我在@BobKing接受的答案下留言:
@BobKing -这似乎在ToolTipService中不起作用。绑定到从SQL sproc中嵌入换行符的字段时的工具提示。
他回答说:
@CodeMaverick如果你绑定到嵌入新行文本,它们可能应该是真正的char 10值(或13),而不是XML哨兵。这仅适用于在XAML文件中写入文字新行。
一个灯泡坏了!
我进入我的SQL函数,将换行符的文本表示替换为…
CHAR( 13 ) + CHAR( 10 )
... 从我的TextBlock删除转换器。文本绑定,就像这样……它工作!
其他回答
解决方案背后的代码
private void Button1_Click(object sender, RoutedEventArgs e)
{
System.Text.StringBuilder myStringBuilder = new System.Text.StringBuilder();
myStringBuilder.Append("Orange").AppendLine();
myStringBuilder.Append("").AppendLine();
myStringBuilder.Append("Apple").AppendLine();
myStringBuilder.Append("Banana").AppendLine();
myStringBuilder.Append("").AppendLine();
myStringBuilder.Append("Plum").AppendLine();
TextBox1.Text = myStringBuilder.ToString();
}
也许有人更喜欢
<TextBlock Text="{Binding StringFormat='Stuff on line1{0}Stuff on line2{0}Stuff on line3',
Source={x:Static s:Environment.NewLine}}" />
xmlns: s =“clr-namespace:系统;装配= mscorlib”。
对于那些已经尝试了这个问题的所有答案,但仍在挠头为什么它们都不适合你的人来说,你可能遇到了我遇到的某种形式的问题。
我的TextBlock。Text属性位于ToolTipService内部。元素,它绑定到一个对象的属性,该对象的数据正在从SQL存储过程中提取。现在,存储过程中这个特定属性的数据正在从一个SQL函数中提取。
因为没有什么对我有用,我放弃了我的搜索,并创建了下面的转换器类:
public class NewLineConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
var s = string.Empty;
if (value.IsNotNull())
{
s = value.ToString();
if (s.Contains("\\r\\n"))
s = s.Replace("\\r\\n", Environment.NewLine);
if (s.Contains("\\n"))
s = s.Replace("\\n", Environment.NewLine);
if (s.Contains("

"))
s = s.Replace("

", Environment.NewLine);
if (s.Contains("
"))
s = s.Replace("
", Environment.NewLine);
if (s.Contains("
"))
s = s.Replace("
", Environment.NewLine);
if (s.Contains(" "))
s = s.Replace(" ", Environment.NewLine);
if (s.Contains(" "))
s = s.Replace(" ", Environment.NewLine);
if (s.Contains(" "))
s = s.Replace(" ", Environment.NewLine);
if (s.Contains("<br />"))
s = s.Replace("<br />", Environment.NewLine);
if (s.Contains("<LineBreak />"))
s = s.Replace("<LineBreak />", Environment.NewLine);
}
return s;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
最后我不得不使用环境。NewLine方法来自@dparker的答案。我指示转换器寻找任何可能的换行符文本表示,并将其替换为Environment.NewLine。
这个工作!
然而,我仍然感到困惑,为什么没有其他方法适用于绑定属性。
我在@BobKing接受的答案下留言:
@BobKing -这似乎在ToolTipService中不起作用。绑定到从SQL sproc中嵌入换行符的字段时的工具提示。
他回答说:
@CodeMaverick如果你绑定到嵌入新行文本,它们可能应该是真正的char 10值(或13),而不是XML哨兵。这仅适用于在XAML文件中写入文字新行。
一个灯泡坏了!
我进入我的SQL函数,将换行符的文本表示替换为…
CHAR( 13 ) + CHAR( 10 )
... 从我的TextBlock删除转换器。文本绑定,就像这样……它工作!
注意,要做到这一点,你需要在文本属性中这样做,你不能使用像这样的内容
<TextBlock>Stuff on line1
Stuff on line 2</TextBlock>
也不工作
<TextBlock><TextBlock.Text>NO USING ABOVE TECHNIQUE HERE</TextBlock.Text>
没什么大不了的,只是需要用一下
<TextBlock Text="Cool 
Newline trick" />
代替。