Hi,

I am using AvalonDock to create a nice UI for my playlist manager. Basically it is an app that allows me to view playlists side by side, drag songs from one to another, and search through multiple playlists at the same time.

In my search window I now want to be able to choose which playlist windows to search through. By default they will all be chosen, but I might not want to search a certain playlist and should then be able to exclude that.

So my idea was to use a ComboBox, where each item is a CheckBox. When checked, the search includes that playlist, so I can simply dropdown the ComboBox and uncheck the playlists I do not want to search in.


The SearchWindow has a reference to the main window (that hosts the playlist windows), and the MainWindow has a PlaylistWindows property that returns the Documents of type PlaylistWindow (so that it excludes things like toolwindows).
In the constructor of the MainWindow I immediately create the SearchWindow (but just hide it until I need it), passing the MainWindow instance along. That gives us:
vb.net Code:
  1. Class MainWindow
  2.  
  3.     Private searchWindow As SearchWindow
  4.  
  5.         Public Sub New()
  6.  
  7.         ' This call is required by the designer.
  8.         InitializeComponent()
  9.  
  10.         ' Add any initialization after the InitializeComponent() call.
  11.         searchWindow = New SearchWindow(Me)
  12.         searchWindow.Name = "searchWindow"
  13.         searchWindow.HideOnClose = True
  14.  
  15.         searchPane.Items.Add(searchWindow)
  16.         searchWindow.Hide()
  17.     End Sub
  18.  
  19.     Public ReadOnly Property PlaylistWindows As IEnumerable(Of PlaylistWindow)
  20.         Get
  21.             Return Me.dockingManager.Documents.OfType(Of PlaylistWindow)()
  22.         End Get
  23.     End Property
  24.  
  25.     '...
  26. End Class
I thought I would now be able to bind the ComboBox to the PlaylistWindows property of the MainWindow reference, so that's what I did:
vb.net Code:
  1. Public Class SearchWindow
  2.  
  3.     Private _MainWindow As MainWindow
  4.  
  5.     Public Sub New(ByVal mainWindow As MainWindow)
  6.         Me.InitializeComponent()
  7.         _MainWindow = mainWindow
  8.  
  9.         playlists.ItemsSource = _MainWindow.PlaylistWindows
  10.     End Sub
(playlists is the ComboBox)

In XAML I defined an ItemTemplate for the ComboBox so that it displays a CheckBox as well as the number of songs in the playlist:
Code:
                <ComboBox Name="playlists" Margin="5">
                    <ComboBox.ItemTemplate>
                        <DataTemplate>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="*" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                            
                                <CheckBox Grid.Column="0" Content="{Binding Title}" />
                                <Label Grid.Column="1" FontSize="10">
                                    <Binding Path="Playlist.Count" StringFormat="Songs: {0}" />
                                </Label> 
                            </Grid>
                        </DataTemplate>
                    </ComboBox.ItemTemplate>
                </ComboBox>
When I now run this, however many playlist windows I open, the ComboBox never displays any. It always has 0 items.


I think I know what the problem is, I just don't know how to fix it. I guess the problem is that the SearchWindow is created before any PlaylistWindows are created. So the binding is initiated with 0 playlist windows and simply never updated.
Is the problem that 'MainWindow.PlaylistWindows' is not a dependency property and thus does automatically update? If so, is there anything I can do about that? I am simply returning the OfType extension method of the Documents property of the AvalonDock dock manager. If that property is not a dependecy property then I don't think I can do anything about it, can I..??

I also tried it the 'old fashioned way' by simply re-binding the ComboBox before it opens, but there does not seem to be any event that is raised before the dropdown opens, so I'm not sure if I can do that either...

Thanks!