2014-05-05 76 views
0

我有一個具有基本文本編輯器功能的應用程序。我使用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> 

回答

2

標識開始創建WPF中的TabControl, 在你的C#代碼<TabControl Height="500" Width="Auto" FontSize="14" Name="Mytabcontrol"> </TabControl>

然後添加類似:

private void AddTabitem() 
{ 
    TabItem ti = new TabItem(); 
    ti.Header = "Tab"; 
    ti.Content = // Your richtextbox; 
    Mytabcontrol.Items.Insert(Mytabcontrol.Items.Count, ti); 
    Mytabcontrol.SelectedIndex = Mytabcontrol.Items.Count - 1; 

然後一個的SelectionChanged事件

Mytabcontrol.SelectionChanged += new SelectionChangedEventHandler(TabSelectionChanged); 
} 

此代碼也可能得到方便:

void TabSelectionChanged(object sender, SelectionChangedEventArgs e) 
{ 
    if (Mytabcontrol.SelectedItem != null) 
    { 
     TabItem ti= Mytabcontrol.SelectedItem as TabItem; // Selected Tab 
     if (tabitem.Content != null) 
     { 
      RichTextBox txt = ti.Content as RichTextBox; // your textbox 
     } 
    } 
} 
+0

@JoJo:這可以工作,我可以打開不同標籤中的文件,但是我無法編輯文件。 – user2495173

+0

@ user2495173你在跟我說話嗎?你發佈了錯誤的答案,我認爲;) – JoJo

+0

@JoJo:不,我試過你的方法,大部分工作除了,我沒有得到文本框Automaticscrollbar,我無法編輯文本框中的任何東西。 – user2495173

0

,您將需要一個TabControl

1)添加一個TabControl到你的窗口

2)在窗口的源代碼文件中添加以下代碼

 //code for adding tabItems to your tabControl    

     //when you open a file, string[] richtextBoxLines are the lines 
     public TabItem TItem(string HeaderText, string[] richtextBoxLines) 
     { 
      TabItem t = new TabItem(); 
      t.Header = HeaderText; 
      RichTextBox r = new RichTextBox(); 

      foreach(string s in richtextBoxLines) 
      { 
       r.Selection.Text += s; 
      } 

      t.Content = r; 
      return t; 
     } 
     //when you just add a tab 
     public TabItem TItem(string HeaderText) 
     { 
      TabItem t = new TabItem(); 
      t.Header = HeaderText; 
      RichTextBox r = new RichTextBox(); 
      t.Content = r; 
      return t; 
     } 

現在,您可以添加如下示例中的標籤

//read text file text to get an array 
string[] allLines = null; //set to value of textfile lines 

//you can add a tab with this text 
myTabControl.Items.Add(TItem("FileName", allLines)); 

//or you could also add a tab by creating a new text file 
myTabControl.Items.Add(TItem("new Textfile"));