Dưới đây là cách để có được văn bản hoàn chỉnh với ngắt dòng rõ ràng.
Lưu ý:
- Bao gồm các lớp sau đây từ Advanced Text Formatting Example trong dự án của bạn:
- CustomTextSource
- FontRendering
- GenericTextProperties
- Có một số hạn chế mà được đề cập trong CustomTextSource lớp học. Tuy nhiên, tôi tin rằng yêu cầu của bạn không bị ảnh hưởng bởi những hạn chế đó.
- Đây chỉ là ví dụ. Bạn có thể muốn sửa đổi mã theo nhu cầu của bạn.
- Mã vẫn sử dụng một hack (mặc dù là một tài khoản phong nha) -
InputTextBox.ViewportWidth
. Bạn có thể muốn kiểm tra xem kết quả cuối cùng là chính xác là như mong muốn.
Xem: Advanced Text Formatting và Advanced Text Formatting Example
Mã mẫu
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:
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;
}
tôi xin lỗi nhưng không thể hiểu được vấn đề stat ement. Có phải bạn muốn sao chép văn bản từ hộp văn bản đó và trong khi dán ở một nơi khác muốn bảo vệ các vị trí được bao bọc? – publicgk
Văn bản hoặc dưới dạng hình ảnh/hình ảnh? –
@publicgk, trường hợp chính xác của tôi là chuyển văn bản sang báo cáo đang hoạt động trong khi vẫn giữ vị trí được bọc để tôi có cùng một gói được hiển thị trên màn hình. Vì vậy, tôi cần nó như là văn bản. – Fbeauche