Click here to Skip to main content
15,886,815 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Guys,

So I am attempting to group data in the Listbox in my MVVM WPF application.
I have read both the following links which is where I have sourced how to start this from:

mvvm-grouping-items-in-listview[^]
karl shifflett - wpf-sample-series-listbox-grouping-sorting-subtotals-and-collapsible-regions/[^]

Now I am getting an issue, that for the life of me I cannot explain as I seem to be doing things pretty much the same (bar a few changes).

The issue
This line:
XML
<ItemsPresenter Visibility="{Binding IsChecked, Converter={StaticResource booleanToVisibilityConverter}, ElementName=btnShowHide}" Grid.Row="1" Margin="11,0,0,3.5" />

Is being marked with an error:
Value cannot be null. Parameter name: element

Now, this does not stop the program compiling but it crashes at run time when I try to load the view.

I appologise now for the following largeish code dump, but below is the XAML for the listbox control I am using.

XML
<ListBox Grid.Row="2" Margin="5" DataContext="{DynamicResource SurveyDatesGroup}"
        ItemsSource="{Binding IsAsync=True}">
      <ListBox.GroupStyle>
         <GroupStyle>
            <GroupStyle.ContainerStyle>
               <Style TargetType="{x:Type GroupItem}">
                  <Setter Property="Template">
                      <Setter.Value>
                           <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Grid>
                                   <Grid.RowDefinitions>
                                        <RowDefinition />
                                        <RowDefinition />
                                   </Grid.RowDefinitions>
                                <Grid removed="Black" x:Name="gridTemplate" Height="26"
				      VerticalAlignment="Center">
                                    <Grid.Resources>
                                       <Style TargetType="{x:Type TextBlock}">
                                          <Setter Property="FontSize" Value="14" />
                                          <Setter Property="Foreground" Value="White" />
                                          <Setter Property="VerticalAlignment" Value="Center" />
                                       </Style>
                                    </Grid.Resources>
                                    <Grid.ColumnDefinitions>
                                         <ColumnDefinition Width="Auto" />
                                         <ColumnDefinition Width="*" />
                                         <ColumnDefinition Width="45" />
                                         <ColumnDefinition Width="30" />
                                         <ColumnDefinition Width="110" />
                                    </Grid.ColumnDefinitions>
                                    <ToggleButton x:Name="btnShowHide" IsChecked="True" 
                                          Margin="3.5" />
                                    <TextBlock Grid.Column="1" Text="{Binding Name}" 
                                          Margin="3.5,0" />
                                    <TextBlock Grid.Column="2" Text="Count:" />
                                    <TextBlock Grid.Column="3" Text="{Binding ItemCount}"
					TextAlignment="Right" Margin="0,0,11,0" />
                                </Grid>
                                <ItemsPresenter Visibility="{Binding IsChecked, Converter={StaticResource booleanToVisibilityConverter}, ElementName=btnShowHide}" Grid.Row="1" Margin="11,0,0,3.5" />
                             </Grid>
                             <ControlTemplate.Triggers>
                                  <DataTrigger Binding="{Binding IsBottomLevel}" Value="True">
                                         <Setter TargetName="gridTemplate" Property="Background" Value="#FF965F00" />
                                   </DataTrigger>
                             </ControlTemplate.Triggers>
                         </ControlTemplate>
                         </Setter.Value>
                     </Setter>
                  </Style>
              </GroupStyle.ContainerStyle>
         </GroupStyle>
     </ListBox.GroupStyle>
     <ListBox.ItemTemplate>
         <DataTemplate>
             <Grid>
                <Grid.ColumnDefinitions>
                     <ColumnDefinition />
                     <ColumnDefinition />
                     <ColumnDefinition />
                 </Grid.ColumnDefinitions>
                 <TextBlock Text="{Binding MQY}"/>
                 <TextBlock Grid.Column="2" Text="{Binding DateRange}"/>
             </Grid>
        </DataTemplate>
    </ListBox.ItemTemplate>
</ListBox>


Any help with solving this would be greatly appreciated, if you need more info let me know.
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900