[Version 1.3]A problem about DocumentsSource

Jun 2, 2010 at 1:18 PM
Edited Jun 3, 2010 at 2:50 PM
I tried the AvalonDock V 1.3 today, the VS2010 theme is really cool, and some problems that exist in V 1.2 is fixed. Thank you for the good work! I found a little problem about DockingManager.DocumentsSource. You can reproduce that problem like this: The XAML code in a window is like this:
<DockPanel>
<Button DockPanel.Dock="Top" Content="add DocumentContent" Click="Button_Click" />
<ava:DockingManager Name="manager" DocumentsSource="{Binding}">
<ava:ResizingPanel>
<ava:DocumentPane></ava:DocumentPane>
<ava:DockablePane>
<ava:DockableContent Title="document list">
<ListBox ItemsSource="{Binding}">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding Path=Title}"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</ava:DockableContent>
</ava:DockablePane>
</ava:ResizingPanel>
</ava:DockingManager>
</DockPanel>


and the code-behind is like this:
public partial class BugTester : Window
{
int documentCount = 0;

ObservableCollection
<DocumentContent> dataSource = new ObservableCollection<DocumentContent>();

public BugTester()
{
InitializeComponent();
manager.DataContext
= dataSource;
}

private void Button_Click(object sender, RoutedEventArgs e)
{
documentCount
++;
dataSource.Clear();
dataSource.Add(
new DocumentContent { Title = "test" + documentCount });
}
}

It's very simple, I created an instance of ObservableCollection<DocumentContent>, the DockingManager.DocumentsSource and the ListBox are both binded to this collection. In the button's Click event handler, I cleared the collection and add one DocumentContent to the collection. So, there will always be only one DocumentContent in the data source, but there will always be two items in the listbox. Hope this problem can be fixed, thank you!
Jun 3, 2010 at 8:46 AM

HI,

it seems a problem related to listbox layout. To prove this just add a call to listDocuments.UpdateLayout() method (I named the listbox with 'listDocuments') after the dataSource.Clear() and everything should work.
I suspect that listbox layout routing is not triggered between the Clear() and the AddNew() (something that is also strange that you can select two lines of the list box with a single click!).

As last note I suggest you to not bind the listbox itemsource to datacontext of the parent docking manager: doing this it won't work when you autohide the content or drag it as floating window (binding will loose source object after it is placed in a different window and that happens when it is autohidden or 'floated').
Instead you should bind the DockingManager DocumentsSource and ListBox ItemsSource to a common source like the window.

Ado

 

Jun 3, 2010 at 2:35 PM
Edited Jun 3, 2010 at 2:51 PM

Hi, ado: I tried to call the ListBox.UpdateLayout() after clearing the dataSource,

and it worked, thank you!

But I still find it strange, because this only happens when both the DockingManager.DocumentsSource

 and the ListBox.ItemsSource are binded to the same data source.

If I remove DocumentsSource="{Binding}" from the XAML code, then the ListBox would act normally, and

 I don't even need to call the UpdateLayout() method.

Would you please explain why is that? Thanks a lot!

Jun 3, 2010 at 3:19 PM

Actually I don't why it happens, maybe something in DockableContent breaks layout routing of the listbox, but I can't be sure of that,

Ado

Jun 5, 2010 at 1:24 AM
Edited Jun 5, 2010 at 1:27 AM

Hi,Ado

Thank you for the reply.

 I tried to move the ListBox out of the DockableContent, and make it a direct sub-element of the root element(in this case, the DockPanel), then the ListBox worked fine(ItemsSource and DocumentsSource were still binded to the same data source).

So I believe what you said could be one of the reasons, but maybe not all of them.

 I'm saying so because I tried another way: keep the ListBox in the DockableContent, remove DocumentsSource="{Binding}" from the XAML, put a DocumentContent in the DocumentPane and put another ListBox in the DocumentContent, make both of the ListBoxes binded to the same data source, then do the clear-before-add thing, and it works fine.

So it seems like the DockableContent is not the only reason why this strange thing is happening, it's somehow related to the DockingManager.DocumentsSource.

Regards!