我对 GroupStyle 和 Expander.IsExpanded 绑定有疑问。我的代码基于这个答案:How to save the IsExpanded state in group headers of a listview from @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>
Run Code Online (Sandbox Code Playgroud)
属性“bIsExpandedGroup”绑定在 DTO_Package 上(绑定到 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));
}
}
(...)
}
Run Code Online (Sandbox Code Playgroud)
此代码有效,但我在 OutputWindow 中遇到此错误:
System.Windows.Data 错误:17:无法从“Items”(类型“ReadOnlyObservableCollection`1”)获取“Item[]”值(类型“Object”)。BindingExpression:Path=Items[0].bIsExpandedGroup; DataItem='CollectionViewGroupInternal'(哈希码=29463315);目标元素是“Expander”(名称=“”);目标属性为“IsExpanded”(类型为“Boolean”) ArgumentOutOfRangeException:“System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。参数名称:索引'
提前感谢您的帮助:)
尝试绑定到Items.CurrentItem.bIsExpandedGroup而不是Items[0].bIsExpandedGroup.
IsExpanded="{Binding Path=Items.CurrentItem.bIsExpandedGroup,
Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
Run Code Online (Sandbox Code Playgroud)
(解释与这里相同)
| 归档时间: |
|
| 查看次数: |
714 次 |
| 最近记录: |