Reputation: 9723
Ok, this is going to be a 1000ft long question, but there's a lot to cover so here goes:
I am creating a paged items control, the purpose of which is to display very large collections in a paged format. I've created a repository on GitHub which can be found here. I have removed any styling for simplicity.
Upon starting the application, it looks like this:
This is pretty straightforward really, there's navigation buttons, an items per page selector but that isn't really important. The problem here is when you click the button "Open New Window".
This will open a new MainWindow
, but on the first window, the collection disappears, as shown below:
The image above shows the old window in front, as you can see, there is no list of content as there is on the new window.
So, after smashing my head against a wall for a couple of hours, I am in need of assistance. I'll provide an overview of how the project is structured.
AnagramPagedItemsControl
The control being used for displaying the content is a custom control called AnagramPagedItemsControl
, it is responsible for handling navigation between pages. I think the key property here is the PagedCollection
.
The PagedCollection
dependency property holds the collection which is bound to the Models
property in the TestItemsViewModel
.
TestItemsViewModel
This is the DataContext
of the MainWindow
, each window instance should create it's own view model. The CreateTestItems()
method is responsible for creating the list of test items.
LazyPagedCollection
The purpose of this collection is to encapsulate the logic of a paged observable collection, it only loads pages when they are needed, hence the laziness.
It exposes methods like NextPage
which are called in the AnagramPagedItemsControl
when the user clicks on the various navigation buttons. The view model can also call navigation on the LazyPagedCollection
, this allows the view model to call navigation without having to go through the view to do it.
TL;DR
When I create a new Window
, the content of the previous window disappears. The problem is almost certainly with the control however I am stuck as to how to fix the problem.
This is quite a large problem to look at so I'd be very grateful for anyone who can look into it. Again, the source code is here, please feel free to suggest alternatives or pick out anything that I may have overlooked.
Upvotes: 0
Views: 61
Reputation: 6570
Had some time to spare, so:
The problem is the setter for the CollectionView
property in the style for AnagramPagedItemsControl
in generic.xaml
.
This does not instantiate a new ListBox
every time the style is applied; it will just create the one ListBox
, the first time the style is created, and use that value over, and over again. So in effect, every instance of MainWindow
shares the same ListBox
.
You can see this by setting the Tag
property of PART_CollectionView
to (for instance) "1" in SetupBindings(ItemsControl PART_CollectionView)
. When you open a new window, you'll see that PART_CollectionView.Tag
contains the same value you previously assigned.
Upvotes: 2