Le ZVince
Le ZVince

Reputation: 61

Problem with GroupStyle and Expander.IsExpanded Binding

I have a problem with GroupStyle and Expander.IsExpanded Binding. I based my code on this answer : How to save the IsExpanded state in group headers of a listview from @user1. I cannot comment on this answer because my reputation is not high enough, so that's why I make this new topic.

In my ListView I have this code :

<!-- Group Container Style -->
<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}">
                            <Expander IsExpanded="{Binding Path=Items[0].bIsExpandedGroup}">
                                <Expander.Header>
                                    <DockPanel>
                                        <TextBlock FontWeight="Bold"
                                            Style="{StaticResource DefaultTextBlockItemStyle}"
                                            Text="{Binding Path=Name}"
                                            />
                                    </DockPanel>
                                </Expander.Header>
                                <Expander.Content>
                                    <ItemsPresenter />
                                </Expander.Content>
                            </Expander>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </GroupStyle.ContainerStyle>
    </GroupStyle>
</ListView.GroupStyle>

The Property "bIsExpandedGroup" is binded on a DTO_Package (objects binded to the ListView)

public class DTO_Package : ViewModelBase
{
    (...)

    private bool _bIsExpandedGroup = false;
    /// <summary>
    /// Used to Expand/Collapse expanders when grouping by Type or Category
    /// 
    /// Exception in the OUTPUT :
    /// System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Object') from 'Items' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=Items[0].bIsExpandedGroup; DataItem='CollectionViewGroupInternal' (HashCode=15134062); target element is 'Expander' (Name=''); target property is 'IsExpanded' (type 'Boolean') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
    /// Parameter name: index'
    /// 
    /// </summary>
    public bool bIsExpandedGroup
    {
        get { return _bIsExpandedGroup; }
        set
        {
            _bIsExpandedGroup = value;
            OnPropertyChanged(nameof(bIsExpandedGroup));
        }
    }

    (...)
}

This code works but I have this error in the OutputWindow :

System.Windows.Data Error: 17 : Cannot get 'Item[]' value (type 'Object') from 'Items' (type 'ReadOnlyObservableCollection`1'). BindingExpression:Path=Items[0].bIsExpandedGroup; DataItem='CollectionViewGroupInternal' (HashCode=29463315); target element is 'Expander' (Name=''); target property is 'IsExpanded' (type 'Boolean') ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: index'

Thanks per advance for your help :)

Upvotes: 2

Views: 838

Answers (1)

Arie
Arie

Reputation: 5373

Try binding to Items.CurrentItem.bIsExpandedGroup instead of Items[0].bIsExpandedGroup.

IsExpanded="{Binding Path=Items.CurrentItem.bIsExpandedGroup, 
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"

(Explanation is the same as here)

Upvotes: 9

Related Questions