2008-11-20 17 views
6

यदि पाठ की एक पंक्ति एक अतिरिक्त रेखा में लपेटा गया है, तो मैं स्ट्रिंग में प्रोग्रामेटिक रूप से बिंदु को कैसे निर्धारित कर सकता हूं जहां यह टूटा हुआ था।डब्ल्यूपीएफ समृद्ध टेक्स्टबॉक्स प्रश्न

उदाहरण: इनपुट स्ट्रिंग = "यह टेक्स्ट की लिपटे रेखा का परीक्षण है"।

 Based on the width of the richTextBox it could display: 


      This is a test of a wrapped line of 
      text. 

मुझे जो निर्धारित करने की आवश्यकता है वह लपेटने वाले शब्द (ओं) की रेखा में ऑफ़सेट है। उपरोक्त मामले में शब्द "पाठ"।

जब मैं समृद्ध टेक्स्टबॉक्स से Xaml निकालता हूं, तो मुझे मूल पाठ अनचाहे मिलता है।

धन्यवाद,

बॉब Kerlinger

+0

आप इस के लिए ऑफसेट का उपयोग करने के कोशिश कर रहे हैं? –

+0

मुझे रचित पाठ को निकालने की आवश्यकता है क्योंकि इसे प्रदर्शित किया गया है ताकि मैं इसे फिर से चला सकूं जैसा कि यह किसी अन्य माध्यम पर बना था। इसके अलावा, कुछ मामलों में, मुझे एक लटकती हुई इंडेंट बनाने की आवश्यकता है जो समृद्ध टेक्स्टबॉक्स समर्थन नहीं करता है। मुझे कुछ अन्य विशेष स्वरूपण भी करना है। –

उत्तर

2

चाल मैंने पाया TextPointer वर्ग और उसके GetCharacterRec विधि का उपयोग करता।

रिचटेक्स्टबॉक्स में फ़्लो डॉक्यूमेंट है। प्रवाह दस्तावेजों में पाठ एक रन ऑब्जेक्ट (सरलीकरण का बिट, लेकिन यह काम करता है) में निहित है। कोड पहले रन की शुरुआत में टेक्स्ट पॉइंटर पाता है। इसके बाद वह उस पहले चरित्र के बाध्य आयत को प्राप्त करता है। इसके बाद कोड एक समय में एक चरित्र को आगे बढ़ाता है, एक नया बाउंडिंग आयत प्राप्त करता है और जांच करता है कि नए आयत के नीचे मूल आयताकार से अलग है या नहीं। यदि नीचे अलग है तो हम एक नई लाइन पर हैं। टेक्स्ट पॉइंटर तो ब्रेक से पहले या बाद में टेक्स्ट प्राप्त कर सकता है।

public partial class Window1 : Window 
{ 
    public Window1() 
    { 
     InitializeComponent(); 
    } 

    private void inspect(object sender, RoutedEventArgs e) 
    { 
     TextPointer pointer = FindRun(inBox.Document); 

     string textAfterBreak = FindBreak(pointer); 

     outBox.Text = textAfterBreak; 
    } 

    private string FindBreak(TextPointer pointer) 
    { 
     Rect rectAtStart = pointer.GetCharacterRect(LogicalDirection.Forward); 

     pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward); 
     Rect currentRect = pointer.GetCharacterRect(LogicalDirection.Forward); 

     while (currentRect.Bottom == rectAtStart.Bottom) 
     { 
      pointer = pointer.GetNextInsertionPosition(LogicalDirection.Forward); 
      currentRect = pointer.GetCharacterRect(LogicalDirection.Forward); 
     } 

     string textBeforeBreak = pointer.GetTextInRun(LogicalDirection.Backward); 
     string textAfterBreak = pointer.GetTextInRun(LogicalDirection.Forward); 

     return textAfterBreak; 
    } 

    private TextPointer FindRun(FlowDocument document) 
    { 
     TextPointer position = document.ContentStart; 

     while (position != null) 
     { 
      if (position.Parent is Run) 
       break; 

      position = position.GetNextContextPosition(LogicalDirection.Forward); 
     } 

     return position; 
    } 
} 

<Window x:Class="LineBreaker.Window1" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    Title="Window1" Height="300" Width="300"> 
    <Grid> 
     <Grid.RowDefinitions> 
      <RowDefinition></RowDefinition> 
      <RowDefinition></RowDefinition> 
      <RowDefinition></RowDefinition> 
     </Grid.RowDefinitions> 
     <RichTextBox Grid.Row="0" Name="inBox"> 
     </RichTextBox> 
     <Button Grid.Row="1" Click="inspect">Find Break</Button> 
     <TextBox Name="outBox" Grid.Row="2"/> 
    </Grid> 
</Window> 
1

http://msdn.microsoft.com/en-us/library/system.windows.documents.textpointer.getlinestartposition.aspx

TextPointer startOfFirstLine = richTextBox.Document.ContentStart; 
TextPointer startOfNextLine = startOfFirstLine.GetLineStartPosition(1); 
if(startOfNextLine != null) 
{ 
    // At this point what you do with the TextPointer depends on what you define as the position of text. 
    // If you want to find out how many characters are on the first line ... 
    int firstLineCharacterCount = new TextRange(startOfFirstLine, startOfNextLine).Text.Length; 
} 
+0

मैंने पहले कोशिश की लेकिन शुरुआतऑफनेक्सलाइन ने शून्य को वापस रखा। ऐसा लगता है कि अगले रन की प्रारंभ स्थिति मिलती है, लेकिन इसे रन ऑब्जेक्ट में ब्रेक नहीं मिलेगा। मुझे कुछ याद आ रहा है। –

+0

मुझे पता है कि मैं पार्टी के लिए थोड़ा देर हो चुकी हूं, लेकिन क्या आपने शुरूआत करने के लिए एक रास्ता तय किया हैऑफनेक्स्टलाइन वापस लौट रहा है? – JessMcintosh

1

लेकिन startOfFirstLine.GetLineStartPosition(1) रिटर्न अशक्त

संबंधित मुद्दे