2010-06-03 101 views
2

我正在使用AvalonDock(link)創建我的應用程序。我有一個工具欄和一個文檔窗格(VisualStudio就像),每個新文檔都包含一個文本框。現在我想爲我的工具欄添加一個Undo按鈕,它將撤消放置在選定文檔上的文本框的更改。它與Visual Studio中的完全相同。AvalonDock文檔綁定

我想要完成的是回答here,但帶有TabControl和Tabs。 mycode的:

<Window x:Class="_app.MainWindow" 
    xmlns:my="clr-namespace:_app" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:ad="clr-namespace:AvalonDock;assembly=AvalonDock" 
    xmlns:osc="clr-namespace:OpenSourceControls;assembly=DockPanelSplitter" 
    Title="MainWindow" Height="350" Width="525" Loaded="Window_Loaded" 
    DataContext="{Binding RelativeSource={RelativeSource Self}}"> 

<Grid > 
    <Grid.RowDefinitions> 
     <RowDefinition Height="24"/> 
     <RowDefinition Height="Auto"/> 
     <RowDefinition Height="*"/> 
     <RowDefinition Height="24"/> 
    </Grid.RowDefinitions> 

    <!--...Menu And Toolbars --> 

    <ToolBarPanel Grid.Row="1" Width="Auto" HorizontalAlignment="Stretch" > 
      <ToolBar> 
       <Button Command="Undo">Undo</Button> 
      </ToolBar> 
    </ToolBarPanel> 

    <ad:DockingManager x:Name="dockManager" Grid.Row="2"> 
     <ad:ResizingPanel Orientation="Vertical"> 
      <ad:ResizingPanel Orientation="Horizontal"> 
       <ad:DockablePane ad:ResizingPanel.ResizeWidth="150"> 
        <ad:DockableContent x:Name="inputContent" Title="Workspace"> 

          <!-- some dockable windows --> 

        </ad:DockableContent> 
       </ad:DockablePane> 

        <!-- here are added the new Documents--> 
       <ad:DocumentPane Name="mainDocPane" ItemsSource="{Binding ..Don't know..}"> 
        <ad:DocumentPane.ItemContainerStyle> 
         <Style TargetType="ad:DocumentContent"> 
          <Setter Property="Content" Value="{Binding Content}"/> 
         </Style> 
        </ad:DocumentPane.ItemContainerStyle> 
       </ad:DocumentPane>     
      </ad:ResizingPanel>   
     </ad:ResizingPanel> 
    </ad:DockingManager> 
</Grid> 

創建新的文檔窗口是這樣的:

private void NewDocument_Click(object sender, RoutedEventArgs e) 
    { 
     string title = "Document" + (dockManager.Documents.Count+1).ToString(); 

     var doc = new Document() { Title = title }; 
     doc.Show(dockManager); 
     doc.Activate(); 
    } 

和Document類看起來是這樣的:

public partial class Document : AvalonDock.DocumentContent 
{ 
    public Document() 
    { 
     InitializeComponent(); 
    } 
} 

XAML:

<ad:DocumentContent x:Class="_ap.Document" 
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:osc="clr-namespace:OpenSourceControls;assembly=DockPanelSplitter" 
    xmlns:ad="clr-namespace:AvalonDock;assembly=AvalonDock"> 

<DockPanel> 
    <TextBox Name="input" AcceptsReturn="True" /> 
</DockPanel> 

所以我想從上面的鏈接應用相同的機制對這段代碼。

感謝您的任何幫助。

回答

-1

首先你要你的命令撤消綁定你的XAML中您的工具箱是:

<Window.CommandBindings> 
    <CommandBinding Command="Undo" Executed="UndoEvent" /> 
</Window.CommandBindings> 

之後,你可以在xaml.cs創建一個新的事件處理程序:

public void UndoEvent(object sender, ExecutedRoutedEventArgs args){ 
    if (DockManager.ActiveDocument.Content is TextBox) { 
     TextBox textBox = (TextBox)DockManager.ActiveDocument.Content; 
     textBox.Undo(); 
    } 
} 

我不確定它是否有效,因爲我沒有測試過它。無論如何,它不應該有太大的不同。

1

See the ApplicaionCommands.Undo

一旦你配合你的撤銷按鈕,其自帶的.NET FW,當文本框具有焦點撤消將於您無需做任何事情到位命令。

相關問題