2012-05-02 94 views
1

我想開發一個像書一樣行爲的WPF應用程序。將有兩個頁面,用戶將能夠編輯它。WPF書樣應用程序

我不能使用FlowDocumentReader,因爲它不是用戶可編輯的。我也考慮過使用移動的richtextbox,這取決於具有焦點的頁面(在FlowDocument中),但是當文本太長時(我不需要滾動條),它沒有分頁行爲,就像FlowDocumentReader有。

我怎麼能做到這一點?

回答

0

XAML:

<Window x:Class="WpfApplication1.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> 
    <Grid.ColumnDefinitions> 
     <ColumnDefinition Width="*" /> 
     <ColumnDefinition Width="*" /> 
    </Grid.ColumnDefinitions> 
    <RichTextBox Margin="5,42,5,5" Name="richTextBox1" FontSize="{Binding ElementName=slider1, Path=Value}" /> 
    <RichTextBox Margin="5,42,5,5" Name="richTextBox2" Grid.Column="1" FontSize="{Binding ElementName=slider1, Path=Value}"/> 
    <Button Content="NextPage" Grid.Column="1" Height="23" Margin="0,12,4,0" Name="button1" VerticalAlignment="Top" HorizontalAlignment="Right" Width="90" Click="button1_Click" /> 
    <Button Content="PreviousPage" Height="23" HorizontalAlignment="Left" Margin="5,12,0,0" Name="button2" VerticalAlignment="Top" Width="94" Click="button2_Click" /> 
    <Slider Height="23" HorizontalAlignment="Left" Margin="204,12,0,0" Name="slider1" VerticalAlignment="Top" Width="100" Grid.ColumnSpan="2" Minimum="8" Maximum="25" Value="12" /> 
</Grid> 
</Window> 

代碼背後:

using System.Collections.Generic; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 


namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for MainWindow.xaml 
    /// </summary> 
    public partial class MainWindow : Window 
    { 
     List<Page> book = new List<Page>(); 
     int pageNumber = -2; 
     public MainWindow() 
     { 
      InitializeComponent(); 

      for(int i=1; i<10; i++) 
       book.Add(new Page("This is page number "+i+"\n\nContent goes in here.")); 

      // open the book on first page 
      button1_Click(null, null); 

     } 

     class Page 
     { 
      public Page(string text) 
      { 
       this.Text = text; 
      } 
      public string Text { get; set; } 
     } 


     public void setTextToPage(ref RichTextBox page, string text) 
     { 
      // Create a FlowDocument to contain content for the RichTextBox. 
      FlowDocument myFlowDoc = new FlowDocument(); 

      // Add paragraphs to the FlowDocument. 
      myFlowDoc.Blocks.Add(new Paragraph(new Run(text))); 

      page.Document = myFlowDoc; 

     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      if (book.Count < (pageNumber + 4)) 
      { 
       return; 
      } 

      pageNumber += 2; 
      setTextToPage(ref richTextBox1, book[pageNumber].Text); 
      setTextToPage(ref richTextBox2, book[pageNumber+1].Text); 

     } 

     private void button2_Click(object sender, RoutedEventArgs e) 
     { 
      if (pageNumber < 2) 
       return; 

      pageNumber -= 2; 
      setTextToPage(ref richTextBox1, book[pageNumber].Text); 
      setTextToPage(ref richTextBox2, book[pageNumber + 1].Text); 
     } 
    } 
} 
+0

謝謝你的回答,但我想文字是在另一個頁面時的行數變得比高度高richtextbox的。我不知道這是否可能 – Peekyou