0
votes

I have the following DataTemplate for a ListView that uses a StackPanel for its ItemsPanelTemplate.

<DataTemplate x:Key="DayTemplate">
    <Border BorderBrush="Black"
            BorderThickness="2"
            CornerRadius="5"
            Width="150"
            Height="440"
            Background="White">
        <StackPanel Orientation="Vertical">
            <Border BorderBrush="Black"
                    BorderThickness="0 0 0 5"
                    Background="White">
                <StackPanel>
                    <StackPanel.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Add Session"
                                      Command="{x:Static cmd:TimetableCommands.AddSession}"/>
                        </ContextMenu>
                    </StackPanel.ContextMenu>
                    <TextBlock FontWeight="Bold"
                               TextAlignment="Center"
                               Text="{Binding Path=DateInfo.Date, Mode=OneWay, Converter={StaticResource DateNoTime}}"/>
                    <TextBlock TextAlignment="Center"
                           FontWeight="Bold"
                           Text="{Binding Path=DateInfo.DayOfWeek}"/>
                </StackPanel>
            </Border>
            <ListBox Name="lbSessions"
                     Background="Transparent"
                     HorizontalAlignment="Center"
                     Visibility="Visible"
                     ItemsSource="{Binding Source={StaticResource SessionList}}"
                     ItemTemplate="{StaticResource SessionTemplate}">
                <ListBox.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Vertical"/>
                    </ItemsPanelTemplate>
                </ListBox.ItemsPanel>
            </ListBox>
        </StackPanel>
    </Border>
</DataTemplate>

I also have the following Collection Source that I'd like to use for the sorting functionality. It is in a seperate Resource Library, referenced correctly in the Resource Library where the DataTemplate is defined.

<CollectionViewSource Source="{Binding Path=Sessions, Mode=OneWay}"
                      x:Key="SessionList">
    <CollectionViewSource.SortDescriptions>
         <scm:SortDescription PropertyName="StartTime"/>
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

When run, no data is displayed, and the output window indicates that the collection source is trying to obtain the data for the list of items from the window level DataContext, not the DataContext the list uses (which is a couple of layers down from the Window ViewModel).

If I change the line

ItemsSource="{Binding Source={StaticResource SessionList}}" 

to

ItemsSource="{Binding Path=Sessions}"

It works fine, though I no longer have the sorting fuctionality of the Collection Source, which I really want to make use of to avoid having to write my own sorting code.

What's going on? and how can I set the correct DataContext on the Collection Source.

1
Move the CollectionViewSource to the <DataTemplate.Resources>. - mm8

1 Answers

0
votes

Is there a reason you need to keep your CollectionViewSource in a separate ResourceDictionary?

Try nesting the CollectionViewSource in the ListBox's Items

<ListBox Name="lbSessions"
         Background="Transparent"
         HorizontalAlignment="Center"
         Visibility="Visible"
         ItemTemplate="{StaticResource SessionTemplate}">
  <ListBox.Items>
    <CollectionViewSource Source="{Binding Path=Sessions, Mode=OneWay}">
      <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="StartTime" />
      </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
  </ListBox.Items>
</ListBox>