2012-02-29 10 views
7

मैं मुसीबत यह एक पता लगाना का एक सा हो रहा है में लंबवत रूप से केंद्रित रखते हुए, उम्मीद है कि किसी की मदद कर सकते हैं।FlowDocument कर्सर RichTextBox

मैं एक RichTextBox के साथ एक WPF परियोजना है।

जैसा कि मैंने लेख संपादित करते समय मैं दस्तावेज़ हमेशा बने रहने के लिए कर्सर लंबवत रूप से केंद्रित करना चाहते हैं।

उदाहरण के लिए एक धक्का ऊपर या नीचे संपादित करते समय, बजाय कर्सर ऊपर जाने के रूप में, मैं पाठ नीचे आने के लिए करना चाहते हैं। इसके परिणामस्वरूप कर्सर अभी भी रह रहा है।

बहुत बहुत धन्यवाद।

+0

आपको लगता है कि .. आपका आवेदन और पाठ बॉक्स ठीक कैसे कर सकते हैं। यदि आपको पाठ की स्थिति बदलने की आवश्यकता है तो आपको शुरुआत में स्थान जोड़ने की आवश्यकता है और पाठ को – om471987

+0

को ट्रिम करने के दौरान इसे पुनर्प्राप्त करने की आवश्यकता है, मुझे लगता है कि मुझे लगभग एक कामकाजी समाधान है लेकिन कुछ भी पोस्ट करने से पहले थोड़ा और टिंकरिंग भी होगा बेहतर दृष्टिकोण देखना पसंद है – shenku

+0

क्या आप मुझे नमूना दिखा सकते हैं? मैं – om471987

उत्तर

0

तो मैं इन पंक्तियों के साथ कुछ कोशिश कर रहा हूँ, लेकिन यह अभी तक काम कर रहा है, बस कुछ और के साथ व्यस्त नहीं मिला है:

TextPointer start = flowDocument.ContentStart; 
     TextPointer caretPosition = RichTextBox1.CaretPosition; 

     var offset = start.GetOffsetToPosition(caretPosition); 
     RichTextBox1.ScrollToVerticalOffset(offset); 
3

सुनिश्चित नहीं हैं कि अगर यह है कि तुम क्या मन में था है, लेकिन यहाँ का एक सबूत है एक RichTextBox के लिए अवधारणा जो उपयोगकर्ता को रखरखाव केंद्रित करती है जहां उपयोगकर्ता इसे रखता है (बॉक्स में क्लिक करता है)।

हालांकि के रूप में ओंकार ने कहा कि अगर दस्तावेज़ आरंभ या अंत आप पाठ स्क्रॉल करने के लिए अनुमति देने के लिए सफेद जोड़ने की जरूरत के लिए स्क्रॉल कर दिया गया है कि आप सफेद स्थान जोड़ने की आवश्यकता होगी।

<RichTextBox HorizontalAlignment="Left" Height="311" VerticalAlignment="Top" Width="509" PreviewKeyDown="HandleKeyDownEvent"> 
      <FlowDocument> 
       <Paragraph Margin="0"> 
        Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla turpis sem, tincidunt id vestibulum venenatis, fermentum eget orci. Donec mollis neque ac leo tincidunt tempus. Pellentesque mollis, nunc sit amet fermentum rutrum, lectus augue ultrices nibh, at lacinia est est ut justo. Cras non quam eu enim vulputate porttitor eu sit amet lectus. Suspendisse potenti. Maecenas metus nunc, dapibus id dapibus rhoncus, semper quis leo. Pellentesque eget risus magna, dignissim aliquam diam. Morbi. 
       </Paragraph> 
      </FlowDocument> 
     </RichTextBox> 

पीछे कोड में:

private void HandleKeyDownEvent(object sender, KeyEventArgs e) 
     { 
      RichTextBox rtb = sender as RichTextBox; 
      if (rtb != null) 
      { 
       //text to scroll up relative to caret 
       if (e.Key == Key.Down) 
       { 
        Block paragraph; 

        //get the whitespace paragraph at end of documnent 
        paragraph = 
          rtb.Document.Blocks 
           .Where(x => x.Name == "lastParagraph") 
           .FirstOrDefault(); 

        // if there is no white space paragraph create it 
        if (paragraph == null) 
        { 
         paragraph = new Paragraph { Name = "lastParagraph", Margin = new Thickness(0) }; 

         //add to the end of the document 
         rtb.Document.Blocks.InsertAfter(rtb.Document.Blocks.LastBlock, paragraph); 
        } 

        // if viewport larger than document, add whitespace content to fill view port 
        if (rtb.ExtentHeight < rtb.ViewportHeight) 
        { 
         Thickness margin = new Thickness() { Top = rtb.ViewportHeight - rtb.ExtentHeight }; 
           margin.Bottom = rtb.ViewportHeight - rtb.ExtentHeight; 
           paragraph.Margin = margin; 

        } 

        // if the document has been scrolled to the end or doesn't fill the view port 
        if (rtb.VerticalOffset + rtb.ViewportHeight == rtb.ExtentHeight) 
        { 
         // and a line to the white paragraph 
         paragraph.ContentEnd.InsertLineBreak(); 
        } 

        //move the text up relative to caret 
        rtb.LineDown(); 
       } 
       // text is to scroll download relative to caret 
       if (e.Key == Key.Up) 
       { 
        // get whitespace at start of document 
        Block paragraph; 
        paragraph = 
          rtb.Document.Blocks 
           .Where(x => x.Name == "firstParagraph") 
           .FirstOrDefault(); 

        //if whitespace paragraph is null append a new one 
        if (paragraph == null) 
        { 
         paragraph = new Paragraph { Name = "firstParagraph", Margin = new Thickness(0) }; 
         rtb.Document.Blocks.InsertBefore(rtb.Document.Blocks.FirstBlock, paragraph); 
        } 

        // up document is at top add white space 
        if (rtb.VerticalOffset == 0.0) 
        { 
         paragraph.ContentStart.InsertLineBreak(); 
        } 

        //move text one line down relative to caret 
        rtb.LineUp(); 
       } 
      } 
     } 

संपादित करें: इस तरीके से काम करने लगता है। रेखा की ऊंचाई को ऑफ़सेट करने में लाइन ब्रेक होने की समस्या से परहेज करते हुए, रेखा की ऊंचाई को अगले पंक्ति में एक पंक्ति के शीर्ष के बीच अंतर का उपयोग करके निर्धारित किया जाता है।

<RichTextBox 
     PreviewKeyDown="PreviewKeyDownHandler"> 
     <FlowDocument> 
      <!-- Place content here --> 
     </FlowDocument> 
    </RichTextBox> 

पीछे कोड में:

private void PreviewKeyDownHandler(object sender, KeyEventArgs e) 
    {    
     RichTextBox rtb = sender as RichTextBox; 
     if (rtb != null) 
     { 
      if (e.Key == Key.Down) 
      { 
       // if there is another line below current 
       if (rtb.CaretPosition.GetLineStartPosition(0) != rtb.CaretPosition.GetLineStartPosition(1)) 
       { 
        // find the FlowDocumentView through reflection 
        FrameworkElement flowDocumentView = GetFlowDocument(rtb); 

        // get the content bounds of the current line 
        Rect currentLineBounds = rtb.CaretPosition.GetCharacterRect(LogicalDirection.Forward); 

        // move the caret down to next line 
        EditingCommands.MoveDownByLine.Execute(null, rtb); 

        // get the content bounds of the new line 
        Rect nextLineBounds = rtb.CaretPosition.GetCharacterRect(LogicalDirection.Forward); 

        // get the offset the document 
        double currentDocumentOffset = flowDocumentView.Margin.Top; 

        // add the height of the previous line to the offset 
        // the character rect of a line doesn't include the baseline offset so the actual height of line has to be determined 
        // from the difference in the offset between the tops of the character rects of the consecutive lines 
        flowDocumentView.Margin = new Thickness { Top = currentDocumentOffset + currentLineBounds.Top - nextLineBounds.Top }; 
       } 

       // prevent default behavior 
       e.Handled = true; 
      } 
      if (e.Key == Key.Up) 
      { 
       if (rtb.CaretPosition.GetLineStartPosition(0) != rtb.CaretPosition.GetLineStartPosition(-1)) 
       { 
        FrameworkElement flowDocumentView = GetFlowDocument(rtb); 

        Rect currentLineBounds = rtb.CaretPosition.GetCharacterRect(LogicalDirection.Forward); 

        EditingCommands.MoveUpByLine.Execute(null, rtb); 

        Rect nextLineBounds = rtb.CaretPosition.GetCharacterRect(LogicalDirection.Forward); 

        double currentDocumentOffset = flowDocumentView.Margin.Top; 

        flowDocumentView.Margin = new Thickness { Top = currentDocumentOffset + currentLineBounds.Top - nextLineBounds.Top }; 
       } 

       e.Handled = true; 
      } 
     } 
    } 

    protected FrameworkElement GetFlowDocument(RichTextBox textBox) 
    { 
     FrameworkElement flowDocumentVisual = 
      GetChildByTypeName(textBox, "FlowDocumentView") as FrameworkElement; 

     return flowDocumentVisual; 
    } 

    protected DependencyObject GetChildByTypeName(DependencyObject dependencyObject, string name) 
    { 
     if (dependencyObject.GetType().Name == name) 
     { 
      return dependencyObject; 
     } 
     else 
     { 
      if (VisualTreeHelper.GetChildrenCount(dependencyObject) > 0) 
      { 
       int childCount = VisualTreeHelper.GetChildrenCount(dependencyObject); 

       for (int idx = 0; idx < childCount; idx++) 
       { 
        var dp = GetChildByTypeName(VisualTreeHelper.GetChild(dependencyObject, idx), name); 
        if (dp != null) 
         return dp; 
       } 

       return null; 
      } 
      else 
      { 
       return null; 
      } 
     } 
    } 
+0

के रास्ते को समझने की कोशिश करता हूं हाय, कीडाउन इवेंट को 'कुंजी। डाउन' कुंजी के लिए निकाल दिया नहीं जाता है जिसे आप जांच रहे हैं। अगर मैं कोड में कदम रखता हूं तो ऐसा कोई प्रभाव नहीं पड़ता है। – shenku

+0

@shenku: हाँ, RichTextBox KeyDown घटना निगल, यही कारण है कि मैं PreviewKeyDown घटना का उपयोग है, यह अर्थ यह पहले यह उठाया RichTextBox के KeyDown हैंडलर को क्रियान्वित करने और RTB द्वारा निगल लिया नहीं है एक सुरंग घटना है। RichTextBox के लिए xaml घोषणा की जांच करें। –

+0

लगभग वहां, यह काम करता है लेकिन समय के साथ जब आप इसे धीरे-धीरे प्राप्त करते हैं, उर्फ ​​स्थान पर रहने के बजाए दस्तावेज़ को ऊपर ले जाता है। लाइन ब्रेक के चारों ओर कूदने के लिए लगता है। : एस – shenku