This project is read-only.

Error while closing a DocumentContent

Aug 30, 2009 at 1:40 PM

Hi,

i am using the new method of binding the DocumentContents via an ObserverableCollection to the DockingManger:

My Xaml ist:

<Window x:Class="AvalonDockTest.Window1"    
        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"    
        Title="Text Document Source" Height="600" Width="800" >
    
    <DockPanel>
        <ToolBar DockPanel.Dock="Top">
            <Button Content="Add Documents" Click="Button_Click"/>
        </ToolBar>
        <ad:DockingManager x:Name="dockingManager" Loaded="dockingManager_Loaded"
DocumentsSource="{Binding Path=MyDocs}" RequestDocumentClose="dockingManager_RequestDocumentClose">
            <ad:DocumentPane x:Name="_documentHost" />
        </ad:DockingManager>
    </DockPanel>
</Window>

 

and my csharp-code from the code behinde:

   public partial class Window1 : Window
    {
        public Window1()
        {
            MyDocs = new ObservableCollection<DocumentContent>();
            this.DataContext = this;
            InitializeComponent();
        }

        public ObservableCollection<DocumentContent> MyDocs { get; set; }

        private void dockingManager_Loaded(object sender, RoutedEventArgs e)
        {
        }


        void doc_Closed(object sender, EventArgs e)
        {
            //System.Diagnostics.Debug.WriteLine("Klosed: ");
            
        }

        void doc_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            //System.Diagnostics.Debug.WriteLine("Klosing: ");
        }


        private void Button_Click(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
               // DemoDocument doc = new DemoDocument();
                DocumentContent doc = new DocumentContent();

                doc.Title = "Document " + (i);
                doc.InfoTip = "Info tipo for " + doc.Title;
                doc.ContentTypeDescription = "Sample document";
                doc.Closing += new EventHandler<System.ComponentModel.CancelEventArgs>(doc_Closing);
                doc.Closed += new EventHandler(doc_Closed);
                MyDocs.Add(doc);
            }
        }


        private void dockingManager_RequestDocumentClose(object sender, RequestDocumentCloseEventArgs e)
        {
            //System.Diagnostics.Debug.WriteLine("Request: ");
            MyDocs.Remove(e.DocumentToClose);
        }

        private void Button1_Click(object sender, RoutedEventArgs e)
        {
          //  dockingManager.m
            MyDocs.Clear();
        }
    }

 

Everthing looks fine until i want to close a document.

Closing a Dokument via clicking the X in the DockingManger do not have an effekt an closing the document via rightClick->close throws an exception!!!

 

What is wrong ?

 

 

 

Sep 4, 2009 at 12:56 PM
Edited Sep 4, 2009 at 12:56 PM

Could you please post the exception?

Ado

 

Sep 9, 2009 at 3:23 PM
Edited Sep 9, 2009 at 3:48 PM

I get this exception when clicking the close button when bound to an ObservableCollection. The error is fairly self-explanatory, but I can't work out how to hook in to the close button click in order to implement things properly. I searched the source but couldnt find where this exception is thrown

 

 System.InvalidOperationException was unhandled

  Message="Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."
  Source="PresentationFramework"
  StackTrace:
       at System.Windows.Controls.ItemCollection.CheckIsUsingInnerView()
       at System.Windows.Controls.ItemCollection.Remove(Object removeItem)
       at AvalonDock.DocumentContent.Close()
       at AvalonDock.DocumentPane.ExecutedCloseCommand(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)
       at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
       at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)
       at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)
       at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)
       at System.Windows.Input.RoutedCommand.ExecuteCore(Object parameter, IInputElement target, Boolean userInitiated)
       at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)
       at System.Windows.Controls.Primitives.ButtonBase.OnClick()
       at System.Windows.Controls.Button.OnClick()
       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)
       at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)
       at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e)
       at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)
       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)
       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)
       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)
       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)
       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)
       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)
       at System.Windows.Input.InputManager.ProcessStagingArea()
       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)
       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)
       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)
       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)
       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)
       at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)
       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)
       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)
       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
       at System.Windows.Threading.Dispatcher.Run()
       at System.Windows.Application.RunDispatcher(Object ignore)
       at System.Windows.Application.RunInternal(Window window)
       at System.Windows.Application.Run(Window window)
       at System.Windows.Application.Run()
       at STUI2.App.Main() in C:\UtilityCode\StarTeam\STUI\STUI2\obj\Debug\App.g.cs:line 0
       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

       at System.Threading.ThreadHelper.ThreadStart()

 

System.InvalidOperationException was unhandled

  Message="Operation is not valid while ItemsSource is in use. Access and modify elements with ItemsControl.ItemsSource instead."

  Source="PresentationFramework"

  StackTrace:

       at System.Windows.Controls.ItemCollection.CheckIsUsingInnerView()

       at System.Windows.Controls.ItemCollection.Remove(Object removeItem)

       at AvalonDock.DocumentContent.Close()

       at AvalonDock.DocumentPane.ExecutedCloseCommand(Object sender, ExecutedRoutedEventArgs e)

       at System.Windows.Input.CommandBinding.OnExecuted(Object sender, ExecutedRoutedEventArgs e)

       at System.Windows.Input.CommandManager.ExecuteCommandBinding(Object sender, ExecutedRoutedEventArgs e, CommandBinding commandBinding)

       at System.Windows.Input.CommandManager.FindCommandBinding(CommandBindingCollection commandBindings, Object sender, RoutedEventArgs e, ICommand command, Boolean execute)

       at System.Windows.Input.CommandManager.FindCommandBinding(Object sender, RoutedEventArgs e, ICommand command, Boolean execute)

       at System.Windows.Input.CommandManager.OnExecuted(Object sender, ExecutedRoutedEventArgs e)

       at System.Windows.UIElement.OnExecutedThunk(Object sender, ExecutedRoutedEventArgs e)

       at System.Windows.Input.ExecutedRoutedEventArgs.InvokeEventHandler(Delegate genericHandler, Object target)

       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)

       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)

       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)

       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)

       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)

       at System.Windows.Input.RoutedCommand.ExecuteImpl(Object parameter, IInputElement target, Boolean userInitiated)

       at System.Windows.Input.RoutedCommand.ExecuteCore(Object parameter, IInputElement target, Boolean userInitiated)

       at MS.Internal.Commands.CommandHelpers.CriticalExecuteCommandSource(ICommandSource commandSource, Boolean userInitiated)

       at System.Windows.Controls.Primitives.ButtonBase.OnClick()

       at System.Windows.Controls.Button.OnClick()

       at System.Windows.Controls.Primitives.ButtonBase.OnMouseLeftButtonUp(MouseButtonEventArgs e)

       at System.Windows.UIElement.OnMouseLeftButtonUpThunk(Object sender, MouseButtonEventArgs e)

       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)

       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)

       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)

       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)

       at System.Windows.UIElement.ReRaiseEventAs(DependencyObject sender, RoutedEventArgs args, RoutedEvent newEvent)

       at System.Windows.UIElement.CrackMouseButtonEventAndReRaiseEvent(DependencyObject sender, MouseButtonEventArgs e)

       at System.Windows.UIElement.OnMouseUpThunk(Object sender, MouseButtonEventArgs e)

       at System.Windows.Input.MouseButtonEventArgs.InvokeEventHandler(Delegate genericHandler, Object genericTarget)

       at System.Windows.RoutedEventArgs.InvokeHandler(Delegate handler, Object target)

       at System.Windows.RoutedEventHandlerInfo.InvokeHandler(Object target, RoutedEventArgs routedEventArgs)

       at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised)

       at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args)

       at System.Windows.UIElement.RaiseEvent(RoutedEventArgs args, Boolean trusted)

       at System.Windows.Input.InputManager.ProcessStagingArea()

       at System.Windows.Input.InputManager.ProcessInput(InputEventArgs input)

       at System.Windows.Input.InputProviderSite.ReportInput(InputReport inputReport)

       at System.Windows.Interop.HwndMouseInputProvider.ReportInput(IntPtr hwnd, InputMode mode, Int32 timestamp, RawMouseActions actions, Int32 x, Int32 y, Int32 wheel)

       at System.Windows.Interop.HwndMouseInputProvider.FilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

       at System.Windows.Interop.HwndSource.InputFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

       at MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)

       at MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)

       at System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Boolean isSingleParameter)

       at System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

       at System.Windows.Threading.Dispatcher.WrappedInvoke(Delegate callback, Object args, Boolean isSingleParameter, Delegate catchHandler)

       at System.Windows.Threading.Dispatcher.InvokeImpl(DispatcherPriority priority, TimeSpan timeout, Delegate method, Object args, Boolean isSingleParameter)

       at System.Windows.Threading.Dispatcher.Invoke(DispatcherPriority priority, Delegate method, Object arg)

       at MS.Win32.HwndSubclass.SubclassWndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam)

       at MS.Win32.UnsafeNativeMethods.DispatchMessage(MSG& msg)

       at System.Windows.Threading.Dispatcher.PushFrameImpl(DispatcherFrame frame)

       at System.Windows.Threading.Dispatcher.PushFrame(DispatcherFrame frame)

       at System.Windows.Threading.Dispatcher.Run()

       at System.Windows.Application.RunDispatcher(Object ignore)

       at System.Windows.Application.RunInternal(Window window)

       at System.Windows.Application.Run(Window window)

       at System.Windows.Application.Run()

       at STUI2.App.Main() in C:\UtilityCode\StarTeam\STUI\STUI2\obj\Debug\App.g.cs:line 0

       at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)

       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)

       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()

       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)

       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)

       at System.Threading.ThreadHelper.ThreadStart()

  InnerException: 

 

 

 

 

Sep 10, 2009 at 11:53 AM

Hi,

It seems that your code comes from the Window4.xaml sample included into the demo project AvalonDockTest.

Using version 1.2.2632 I made some test and I found:

1. If you close the documentcontent with the 'X' it is correctly closed and removed from the private collection.

2. If you right click on the tab and than choose the 'close this' menu item it raises a nullreferenceexception. I corrected this bug in my internal version.

A short workaround is to replace the line 141 to

from:   if (e.Parameter != null)

to:       if (e.Parameter is ManagedContent)

Thanks,

Ado

Sep 10, 2009 at 11:59 AM

Actually it's just a test app I created from scratch, but I will take a look at the sample app you mentioned - sounds like it will point me in the right direction.

(I am binding the ItemsSource of a DocumentPane to this:

                       {
                         new DocumentContent {Title = "1", Content = new TextBox {Text = "1"}},
                         new DocumentContent {Title = "Test2", Content = new TextBox {Text = "2"}},
                         new DocumentContent {Title = "Test3", Content = new TextBox {Text = "3"}},
                         new DocumentContent {Title = "Test4", Content = new TextBox {Text = "4"}}
                       };
_documents = new ObservableCollection<DocumentContent>
                       {
                         new DocumentContent {Title = "1", Content = new TextBox {Text = "1"}},
                         new DocumentContent {Title = "Test2", Content = new TextBox {Text = "2"}},
                         new DocumentContent {Title = "Test3", Content = new TextBox {Text = "3"}},
                         new DocumentContent {Title = "Test4", Content = new TextBox {Text = "4"}}

                       };

)

Sep 10, 2009 at 12:36 PM

Yes but you cant bind the DocumentPane itemsource property otherwise you'll get the exception you wrote above.

To bind an internal collection of DocumentContents you must use the DockingManager.DocumentsSource property like the example xaml you posted!

other info on my blog http://www.youdev.net/post/2009/07/17/avalondock-documents.aspx

 

Sep 10, 2009 at 12:50 PM

Ah okay - nope I'm not the poster of the first message in this thread. Confusion explained!

Sep 10, 2009 at 2:29 PM
Edited Sep 10, 2009 at 4:38 PM

I searched and couldn't find any property DocumentsSource anywhere in AvalonDock.dll.


Ah, ok. This property is in a newer version of AvalonDock than the one on the download page. 

I downloaded the latest code from source control, built this, and the DocumentsSource property works!