WPF: Get “wrapped” text out of a textbox

后端 未结 4 714
执笔经年
执笔经年 2021-01-18 18:46

Is there a way in WPF to get the text formatted as it display on the textbox when TextWrapping=\"Wrap\"?



        
4条回答
  •  青春惊慌失措
    2021-01-18 19:03

    Here's how to get the complete text with apparent line breaks.

    Note:

    • Include the following classes from Advanced Text Formatting Example in your project:
      • CustomTextSource
      • FontRendering
      • GenericTextProperties
    • There are some limitations which are mentioned in CustomTextSource class. However, I believe your requirements aren't impacted by those limitations.
    • These are just examples. You may want to modify code as per your needs.
    • The code still uses a hack (although a decent one) - InputTextBox.ViewportWidth. You may want to test if the final output is exactly as desired.

    See: Advanced Text Formatting and Advanced Text Formatting Example

    Sample Code
    XAML:

    
        
            
            
            

    Codebehind:

    private void CopyButton_Click(object sender, RoutedEventArgs e)
    {
        List 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 GetTextAsStringList()
    {
        List stringList = new List();
        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;
    }
    

提交回复
热议问题