c# - GroupStyle 和 Expander.IsExpanded 绑定(bind)的问题

标签 c# wpf xaml mvvm

我对 GroupStyle 和 Expander.IsExpanded 绑定(bind)有疑问。我的代码基于这个答案:How to save the IsExpanded state in group headers of a listview来自@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”绑定(bind)在 DTO_Package 上(对象绑定(bind)到 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));
        }
    }

    (...)
}

此代码有效,但我在 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'



感谢您的帮助:)

最佳答案

尝试绑定(bind)到 Items.CurrentItem.bIsExpandedGroup而不是 Items[0].bIsExpandedGroup .

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

(解释同here)

关于c# - GroupStyle 和 Expander.IsExpanded 绑定(bind)的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57412422/

相关文章:

WPF BitmapImage 宽度/高度始终为 1?

c# - XAML 数据网格文本列 : display an empty cell when condition is false

c# - 加载作为我的解决方案一部分的 FlowDocument.xaml

c# - :active pseudoclass with animations sometimes prevents postbacks in Chrome and IE

c# - 单元测试 GetManifestResourceStream

c# - 如何在TextBox中书写时自动添加点

.net - 分层企业应用程序中的 WPF MVVM 架构

c# - WPF: 'ListView.ItemsSource' 属性在 'Loaded' 事件期间为空

c# - 无论如何需要 DBNull 做什么?

C# - foreach 循环 - 良好实践