WPF:获取"包装"文字出一个文本框的 [英] WPF: Get "wrapped" text out of a textbox
本文介绍了WPF:获取"包装"文字出一个文本框的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有没有办法在WPF得到格式化,因为它显示在文本框时TextWrapping =自动换行的文字?
Is there a way in WPF to get the text formatted as it display on the textbox when TextWrapping="Wrap"?
<TextBox Width="200"
TextWrapping="Wrap"
VerticalScrollBarVisibility="Auto"
HorizontalScrollBarVisibility="Auto" />
我试图用的TextFormatter类,但它ONTY让我绘制文本到绘图背景下我只需要和换行符包含文本。
I've tried to use TextFormatter class, but it onty allow me to draw the text to a drawing context where i only need the text with line break included.
推荐答案
下面是如何让具有明显的换行的完整文本。
Here's how to get the complete text with apparent line breaks.
请注意:
- 包括以下从<一类href="http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=wpfsamples&DownloadId=10113"相对=nofollow>高级文本格式为例在您的项目:
- CustomTextSource
- FontRendering
- GenericTextProperties
- Include the following classes from Advanced Text Formatting Example in your project:
- CustomTextSource
- FontRendering
- GenericTextProperties
请参阅:高级文本格式和<一href="http://archive.msdn.microsoft.com/Project/Download/FileDownload.aspx?ProjectName=wpfsamples&DownloadId=10113"相对=nofollow>高级文本格式为例
样品code
XAML:Sample Code
XAML:<Window x:Class="TextFormatterForWrappedText.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525"> <Grid> <TextBox Width="200" x:Name="InputTextBox" TextWrapping="Wrap" VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" Margin="23,12,280,241" /> <TextBox x:Name="FormattedDisplayTextBox" Height="172" HorizontalAlignment="Left" VerticalAlignment="Top" Margin="23,105,0,0" Width="438" AcceptsReturn="True" TextWrapping="Wrap" /> <Button HorizontalAlignment="Left" VerticalAlignment="Top" Margin="257,12,0,0" Height="23" Content="Copy" Name="CopyButton" Width="129" Click="CopyButton_Click" /> </Grid> </Window>
codebehind:
Codebehind:
private void CopyButton_Click(object sender, RoutedEventArgs e) { List<string> stringList = GetTextAsStringList(); StringBuilder sb = new StringBuilder(); foreach (string s in stringList) { sb.Append(s); sb.Append("\r\n"); } Clipboard.SetData(System.Windows.DataFormats.Text, sb.ToString()); FormattedDisplayTextBox.Clear(); FormattedDisplayTextBox.Text = sb.ToString(); } private List<string> GetTextAsStringList() { List<string> stringList = new List<string>(); int pos = 0; string inputText = InputTextBox.Text; CustomTextSource store = new CustomTextSource(); store.Text = inputText; store.FontRendering = new FontRendering(InputTextBox.FontSize, InputTextBox.TextAlignment, null, InputTextBox.Foreground, new Typeface(InputTextBox.FontFamily, InputTextBox.FontStyle, InputTextBox.FontWeight, InputTextBox.FontStretch)); using (TextFormatter formatter = TextFormatter.Create()) { while (pos < store.Text.Length) { using (TextLine line = formatter.FormatLine(store, pos, InputTextBox.ViewportWidth, new GenericTextParagraphProperties( store.FontRendering), null)) { stringList.Add(inputText.Substring(pos, line.Length - 1)); pos += line.Length; } } } return stringList; }
这篇关于WPF:获取&QUOT;包装&QUOT;文字出一个文本框的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文