簡體   English   中英

WPF:從文本框中獲取“包裝”文本

[英]WPF: Get “wrapped” text out of a textbox

當TextWrapping =“Wrap”時,WPF中是否有一種方法可以將文本格式化為文本框中顯示的格式?

<TextBox   Width="200"
           TextWrapping="Wrap"                 
           VerticalScrollBarVisibility="Auto"
           HorizontalScrollBarVisibility="Auto"  />

我曾嘗試使用TextFormatter類,但是它允許我將文本繪制到繪圖上下文中,其中我只需要包含換行符的文本。

以下是如何獲得具有明顯換行符的完整文本。

注意:

  • 在項目中包含高級文本格式示例中的以下類:
    • CustomTextSource
    • FontRendering
    • GenericTextProperties
  • CustomTextSource類中提到了一些限制。 但是,我相信您的要求不受這些限制的影響。
  • 這些只是一些例子。 您可能希望根據需要修改代碼。
  • 代碼仍然使用hack(雖然是一個不錯的) - InputTextBox.ViewportWidth 您可能想要測試最終輸出是否完全符合要求。

請參閱: 高級文本格式高級文本格式示例

示例代碼
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>

代碼隱藏:

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;
}

請參閱Ian Griffiths對此問題的回答: 從TextBlock中獲取顯示的文本

它從TextBlock獲取顯示的文本(因為它顯示在屏幕上),但我認為你應該能夠將它用於TextBox

為此,您必須使用文本測量API編寫自己的邏輯。

第1將文本框文本刷新為單詞。

步驟2:然后測量每個字寬並將它們組合,直到線寬小於文本框寬度。

請參閱此文章,其中介紹了文本測量過程。 (social.msdn.microsoft.com/forums/en-US/wpf/thread/...)

如果您想要的只是文本框的文本(完整文本而不僅僅是可見部分),要在某個文本塊的同一窗口中顯示為文本(帶有明顯的換行符), 快速入侵可能是:

FormattedText ft = new FormattedText(textBox1.Text,
    System.Globalization.CultureInfo.CurrentCulture,
    textBox1.FlowDirection,
    new Typeface(textBox1.FontFamily,
        textBox1.FontStyle,
        textBox1.FontWeight,
        textBox1.FontStretch),
    textBox1.FontSize,
    textBox1.Foreground);
ft.TextAlignment = textBox1.TextAlignment;
ft.Trimming = TextTrimming.None;

ft.MaxTextWidth = textBox1.ViewportWidth;

textBlock1.Width = textBox1.ViewportWidth;
textBlock1.Height = ft.Height;

textBlock1.TextAlignment = textBox1.TextAlignment;
textBlock1.TextWrapping = textBox1.TextWrapping;
textBlock1.Text = textBox1.Text;

如果在其他地方需要它,您可以將值傳送到該位置並在那里的文本塊上使用它們。

如果您需要完整的文本(帶有明顯的換行符)作為字符串列表(例如List<string> ),其中每個項目代表視線,您將需要一個復雜的解決方案。
此外,如果您只需要文本框中顯示的文本的可見部分,則需要一些復雜的解決方案。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM