C# GroupStyle和Expander.IsExpanded绑定存在问题

C# GroupStyle和Expander.IsExpanded绑定存在问题,c#,wpf,xaml,mvvm,C#,Wpf,Xaml,Mvvm,GroupStyle和Expander.IsExpanded绑定有问题。我的代码基于这个答案:来自@user1。我不能对这个答案发表评论,因为我的声誉不够高,所以我提出了这个新话题 在我的ListView中,我有以下代码: <!-- Group Container Style --> <ListView.GroupStyle> <GroupStyle> <GroupStyle.ContainerStyle>

GroupStyle和Expander.IsExpanded绑定有问题。我的代码基于这个答案:来自@user1。我不能对这个答案发表评论,因为我的声誉不够高,所以我提出了这个新话题

在我的ListView中,我有以下代码:

<!-- 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>

属性“bIsExpandedGroup”绑定在DTO_包上(绑定到ListView的对象)

公共类DTO_包:ViewModelBase
{
(...)
private bool_biexpandedgroup=false;
/// 
///按类型或类别分组时用于展开/折叠展开器
/// 
///输出中的异常:
///System.Windows.Data错误:17:无法从“Items”(类型“ReadOnlyObservableCollection`1”)获取“Item[]”值(类型“Object”)。BindingExpression:Path=Items[0]。bIsExpandedGroup;DataItem='CollectionViewGroupInternal'(HashCode=15134062);目标元素为“Expander”(名称=“”);目标属性为“IsExpanded”(类型“Boolean”)ArgumentOutOfRangeException:'System.ArgumentOutOfRangeException:指定的参数超出有效值的范围。
///参数名称:索引'
/// 
/// 
公共布尔双峰群
{
获取{return\u biexpandedgroup;}
设置
{
_bIsExpandedGroup=值;
OnPropertyChanged(名称(biExpandedGroup));
}
}
(...)
}
此代码正常工作,但OutputWindow中出现以下错误:

System.Windows.Data错误:17:无法从“Items”(类型“ReadOnlyObservableCollection”“1”)获取“Item[]”值(类型“Object”)。BindingExpression:Path=Items[0]。bIsExpandedGroup;DataItem='CollectionViewGroupInternal'(HashCode=29463315);目标元素是“扩展器”(名称=“”);目标属性为“IsExpanded”(类型为“Boolean”)ArgumentOutOfRangeException:“System.ArgumentOutOfRangeException:指定的参数超出有效值的范围。 参数名称:索引'


感谢您的帮助:)

尝试绑定到
项目。CurrentItem.bIsExpandedGroup
而不是
项目[0]。bIsExpandedGroup

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

(解释与相同)

谢谢您的回答,除非它有效!:)很抱歉,我没有足够的声誉为你的答案投+1票。@LeZvince如果它有效,你应该接受答案:)很高兴我能帮上忙。
IsExpanded="{Binding Path=Items.CurrentItem.bIsExpandedGroup, 
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"