gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 11:01:57 50 4
gpt4 key购买 nike

我对 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/

50 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com