我有一個具有基本文本編輯器功能的應用程序。我使用System.Windows.Controls richtextbox來打開文件。目前我的應用程序一次只能打開一個文件。不過,我想增強應用程序以在多個選項卡中打開多重文件。我如何實現多個標籤功能?我很新的WPF框架。任何幫助表示讚賞。我的代碼如下:動態創建WPF richtextbox
<Window x:Class="Editor.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="Pattern Editor" Height="400" Width="600"
>
<Grid>
<!-- Set the styles for the tool bar. -->
<Grid.Resources>
<Style TargetType="{x:Type Button}" x:Key="formatTextStyle">
<Setter Property="FontFamily" Value="Palatino Linotype"></Setter>
<Setter Property="Width" Value="30"></Setter>
<Setter Property="FontSize" Value ="14"></Setter>
<Setter Property="CommandTarget" Value="{Binding ElementName=mainRTB}"></Setter>
</Style>
<Style TargetType="{x:Type MenuItem}" x:Key="formatImageStyle">
<Setter Property="Width" Value="30"></Setter>
<Setter Property="CommandTarget" Value="{Binding ElementName=mainRTB}"></Setter>
</Style>
</Grid.Resources>
<DockPanel Name="mainPanel" >
<Menu DockPanel.Dock="Top">
<MenuItem Header="_File">
<MenuItem Header="_New" Click="New_Click"/>
<Separator />
<MenuItem Header="_Open" Click="Open_Click"/>
<Separator />
<MenuItem Header="_Save" Command="ApplicationCommands.Save" ToolTip="Save" Click="Save_Click">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\FileSave.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Save As" Click="Save_As_Click">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\FileSaveAs.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<Separator />
<MenuItem Header="_Close" Click="Close_Click"/>
<Separator />
<MenuItem Header="_Exit" Click="Exit_Click"/>
</MenuItem>
<MenuItem Header="_Edit">
<MenuItem Header="_Cut" Command="ApplicationCommands.Cut" ToolTip="Cut">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\EditCut.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Copy" Command="ApplicationCommands.Copy" ToolTip="Copy">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\EditCopy.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Paste" Command="ApplicationCommands.Paste" ToolTip="Paste">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\EditPaste.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Undo" Command="ApplicationCommands.Undo" ToolTip="Undo">
<MenuItem.Icon>
<Image Source="C:\Users\stambi\Documents\Visual Studio 2012\Projects\PatternEditor\PatternEditor\Images\EditUndo.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="_Redo" Command="ApplicationCommands.Redo" ToolTip="Redo">
<MenuItem.Icon>
<Image Source="C:\Users\Documents\Visual Studio 2012\Projects\Editor\Editor\Images\EditRedo.png" Height="21"></Image>
</MenuItem.Icon>
</MenuItem>
</MenuItem>
<MenuItem Header="_Tools">
<MenuItem Header="_DisplayFormat">
<MenuItem Header="_Binary" IsCheckable="True"/>
<MenuItem Header="_Hexadecimal" IsCheckable="True"/>
</MenuItem>
<MenuItem Header="_Split" IsCheckable="True" Click="Split_Click"/>
<MenuItem Header="_HighLight"/>
<MenuItem Header="_Show Linenumbers"/>
</MenuItem>
</Menu>
<!--By default pressing tab moves focus to the next control. Setting AcceptsTab to true allows the
RichTextBox to accept tab characters. -->
<RichTextBox Name="mainRTB" AcceptsTab="True" FontFamily="Arial" FontStretch="Normal" VerticalScrollBarVisibility="Auto"></RichTextBox>
</DockPanel>
</Grid>
</Window>
@JoJo:這可以工作,我可以打開不同標籤中的文件,但是我無法編輯文件。 – user2495173
@ user2495173你在跟我說話嗎?你發佈了錯誤的答案,我認爲;) – JoJo
@JoJo:不,我試過你的方法,大部分工作除了,我沒有得到文本框Automaticscrollbar,我無法編輯文本框中的任何東西。 – user2495173