3

To get some context this question refers to How to save the IsExpanded state in group headers of a listview

What am I missing here?

When I try to do what is suggested in the referenced post I get:

BindingExpression path error: '[]' property not found on 'object' ''ExpandStateManager'

My ExpandStateManager

public class ExpandStateManager
{
    Dictionary<Category, bool> expandStates = new Dictionary<Category, bool>();

    public bool this[Category key]
    {
        get
        {
            if (!expandStates.ContainsKey(key)) return false;
            return expandStates[key];
        }
        set
        {
            expandStates[key] = value;
        }
    }
}

XAML

    <ListView ItemsSource="{Binding Source={StaticResource cvs}}"  ItemTemplate="{StaticResource animalTemplate}"
     Width="200">
        <ListView.GroupStyle>
            <GroupStyle>
                <GroupStyle.ContainerStyle>
                    <Style TargetType="{x:Type GroupItem}">
                        <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type GroupItem}">
                                    <Expander IsExpanded="{Binding Source={StaticResource ExpandStateManger}, Path=[Items[0].ObjectType]}">
                                        <Expander.Header>
                                            <DockPanel>
                                                <TextBlock Text="{Binding Name}" />
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                    </Expander>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>
                </GroupStyle.ContainerStyle>
            </GroupStyle>
        </ListView.GroupStyle>
    </ListView>
Community
  • 1
  • 1
foo
  • 1,495
  • 2
  • 11
  • 15

1 Answers1

5

Here is how I solved similar problem:

<ListView  x:Name="listViewOpportunitiesHistory" ItemsSource="{Binding}" AlternationCount="2" 
    IsTextSearchEnabled="False" IsSynchronizedWithCurrentItem="True"   >
    <ListView.GroupStyle>
        <GroupStyle>
            <GroupStyle.ContainerStyle>
                <Style TargetType="{x:Type GroupItem}">
                    <Setter Property="Margin" Value="0,0,0,5"/>
                    <Setter Property="Template">
                        <Setter.Value>
                            <ControlTemplate TargetType="{x:Type GroupItem}">
                                <Grid x:Name="grid_ControlTemplate_listViewOpportunitiesHistory" 
                                    Loaded="grid_ControlTemplate_listViewOpportunitiesHistory_Loaded">
                                    <Expander  BorderBrush="#FFA4B97F" BorderThickness="0,0,0,1"  
                                        x:Name="expanderControlTemplate" 
                                        Expanded="expanderExpandCollapse_Expanded" 
                                        Collapsed="expanderExpandCollapse_Collapsed" >
                                        <Expander.Header>
                                            <DockPanel>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=Name}" Margin="5,0" Width="Auto"/>
                                                <TextBlock FontWeight="Bold" Text="{Binding Path=ItemCount}"/>
                                                <TextBlock FontWeight="Bold" Text=" Items"/>
                                            </DockPanel>
                                        </Expander.Header>
                                        <Expander.Content>
                                            <ItemsPresenter />
                                        </Expander.Content>
                                    </Expander>
                                </Grid>
                            </ControlTemplate>
                        </Setter.Value>
                    </Setter>
                </Style>
            </GroupStyle.ContainerStyle>
        </GroupStyle>
    </ListView.GroupStyle>
    <ListView.View>
        <GridView>
            <GridView.Columns>      
               <GridViewColumn Width="Auto" x:Name="listViewOpportunitiesHistoryColumn_Time"  >
                    <GridViewColumnHeader Click="SortClickHistory" Tag="Time" Content=" Time "
                        x:Name="listViewOpportunitiesHistoryColumnHeader_Time"  />
                    <GridViewColumn.CellTemplate >
                        <DataTemplate>
                            <Border BorderBrush ="Gray" BorderThickness="0,0,1,0" Margin="-6,0,-6,0">
                                <Grid Margin="6,0,6,0" >
                                    <TextBlock Text="{Binding Path=Time, StringFormat='yyyy-MM-dd HH:mm:ss.fff'}" 
                                        Grid.Column ="0" TextTrimming="CharacterEllipsis" 
                                        VerticalAlignment="Center" HorizontalAlignment="Left" 
                                        ToolTipService.ToolTip="{Binding Path=Time}" />
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
               <GridViewColumn Width="Auto" x:Name="listViewOpportunitiesHistoryColumn_AOValue"  >
                    <GridViewColumnHeader Click="SortClickHistory" Tag="AOValue" Content=" AO Value "
                        x:Name="listViewOpportunitiesHistoryColumnHeader_AOValue"  />
                    <GridViewColumn.CellTemplate >
                        <DataTemplate>
                            <Border BorderBrush ="Gray" BorderThickness="0,0,1,0" Margin="-6,0,-6,0">
                                <Grid Margin="6,0,6,0" >
                                    <TextBlock Text="{Binding Path=AOValue}" Grid.Column ="0" TextTrimming="CharacterEllipsis"
                                        VerticalAlignment="Center" HorizontalAlignment="Left" 
                                        ToolTipService.ToolTip="{Binding Path=AOValue}" />
                                </Grid>
                            </Border>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>       
            </GridView.Columns>
        </GridView>
    </ListView.View>  
</ListView>

Code behind:

private Dictionary<string, bool?> expandStates = new Dictionary<string, bool?>();

private void grid_ControlTemplate_listViewOpportunitiesHistory_Loaded(object sender, 
    RoutedEventArgs e)
{
    var grid = (Grid)sender;
    var dc = grid.DataContext as CollectionViewGroup;
    var groupName = (string)dc.Name.ToString();

    //If the dictionary contains the current group, 
    //retrieve a saved state of the group
    if (this.expandStates.ContainsKey(groupName))
    {
        var expander = (Expander)grid.FindName("expanderControlTemplate");
        //btn.IsExpanded = this.expandStates[groupName];
        if (this.expandStates[groupName] == true)
        {
            expander.IsExpanded = true;
        }
        if (this.expandStates[groupName] == false)
        {
            expander.IsExpanded = false;
        }

    }     
}     

private void expanderExpandCollapse_Collapsed(object sender, RoutedEventArgs e)
{
    var expander = (Expander)sender;
    var dc = (CollectionViewGroup)expander.DataContext;
    var groupName = (string)dc.Name.ToString();

    //Loaded event is fired earlier than the Click event, 
    //so I'm sure that the dictionary contains the key
    this.expandStates[groupName] = expander.IsExpanded; //Save the current state
}

private void expanderExpandCollapse_Expanded(object sender, RoutedEventArgs e)
{
    var expander = (Expander)sender;
    var dc = (CollectionViewGroup)expander.DataContext;
    var groupName = (string)dc.Name.ToString();

    //Loaded event is fired earlier than the Click event, 
    //so I'm sure that the dictionary contains the key
    this.expandStates[groupName] = expander.IsExpanded; //Save the current state
}

I get the data from DataSet:

ICollectionView view = CollectionViewSource.GetDefaultView(dataSet.Tables[0]); 
view.GroupDescriptions.Add(new 
    PropertyGroupDescription(FXH.string_GroupBy_OpportunitiesHistory));
     listViewOpportunitiesHistory.ItemsSource = view;

Just remove some unneeded parts of code. My solution is partially based on this StackOverflow question.

Yann Duran
  • 3,842
  • 1
  • 24
  • 24
as74
  • 700
  • 4
  • 12
  • 25
  • This is excellent. Don't know how many hours I've wasted on this before finally finding this post. This solution worked great! – Nik Feb 08 '18 at 03:23