我想开发一个像书一样的WPF应用程序。将有两个页面,用户可以编辑它。
我不能使用FlowDocumentReader,因为它不是用户可编辑的。我还考虑过根据具有焦点的页面(在FlowDocument中)使用移动的richtextbox,但是当文本太长(我不想要滚动条)时它没有像FlowDocumentReader那样的分页行为
我怎么能实现这个目标?
答案 0 :(得分:1)
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);
}
}
}