views:

408

answers:

2

Hi, I'm using AvalonDock (link) to create my application. I have a toolbar and a documentpane (VisualStudio like) and each new document contains a textbox. And now I'd like to add an Undo button to my toolbar which will undo changes on the textbox wich is placed on the selected document.It's completely same like it's in Visual Studio.

What I'd like to accomplish is answered here but with TabControl and 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>

I create new document windows like this:

    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();
    }

And the document class looks like this:

 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>

So I'd like to apply the same mechanism from the link above on this code.

Thanks for any help.

A: 

First you have to bind your command Undo inside your xaml where your toolbox is in:

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

After that, you can create a new eventhandler in the xaml.cs:

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

Im not sure if it works, as I haven't tested it. It shouldn't be much different anyway.

Jhnddy
A: 

See the ApplicaionCommands.Undo

Once you tie your Undo button to the in place command which comes with the .NET FW, when the TextBox has focus the undo will take place without you having to do anything.

Aaron