【发布时间】:2021-07-09 23:50:58
【问题描述】:
我有一个看似相对简单的问题,但我想不通。
我有一个 Xamarin ListView,它使用 ViewCells 的项目模板,如下所示:
<ListView HasUnevenRows="True"
ItemsSource="{Binding Devices}"
ItemTemplate="{StaticResource DeviceDataTemplateSelector}"
SelectionMode="None"
ItemTapped="NewItemTapped"
/>
目前,“DeviceDataTemplateSelector”始终返回以下 ViewCell(稍后将添加更多 ViewCell)。
ViewCell 使用 Xamarin 社区工具包中的 Expander 控件
<ViewCell xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
xmlns:xct="http://xamarin.com/schemas/2020/toolkit"
xmlns:ViewModel="clr-namespace:App.ViewModels"
x:Class="App.UI.ViewCellStandardTemplate">
<ViewCell.View>
<!-- Calls an expander for each ViewCell. -->
<xct:Expander HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="{StaticResource UniversalMargin}"
>
<xct:Expander.Header>
<Grid HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="{StaticResource UniversalMargin}">
<Grid.RowDefinitions>
<RowDefinition Height = "*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "*" />
</Grid.ColumnDefinitions>
<Label Grid.Row="0" Grid.Column="0" Text="Header Text" />
</Grid>
</xct:Expander.Header>
<xct:Expander.Content>
<Grid HorizontalOptions="FillAndExpand"
VerticalOptions="FillAndExpand"
Margin="{StaticResource UniversalMargin}">
<Grid.RowDefinitions>
<RowDefinition Height = "*" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width = "*" />
</Grid.ColumnDefinitions>
<Button Grid.Row="0"
Grid.Column="0"
Text="Click Me"
Command="{Binding Source={RelativeSource AncestorType={x:Type ViewModel:PageViewModel}, Mode=FindAncestorBindingContext}, Path=ClickCommand}"
CommandParameter="{Binding .}"
/>
</Grid>
</xct:Expander.Content>
</xct:Expander>
</ViewCell.View>
</ViewCell>
到目前为止,所有这些都正常工作 - 问题是当我点击 ListView 中的一个项目时,我希望任何其他展开的项目都折叠起来。
换句话说,当我点击一个 ViewCell 来展开它时,我希望 ListView 中的所有其他 ViewCell 将它们各自的 Expander 控件的“IsExpanded”属性设置为 false。
我已尝试在 ListView 中调用“ItemTapped”事件,然后循环浏览 ListView 中的元素 - 我不确定这是否是正确的方法?
在 ViewCell 中访问 Expander 控件属性的最佳方式是什么?
我不知道该怎么做 - 任何帮助将不胜感激。
谢谢。
【问题讨论】:
-
当您“循环遍历 ListView 中的元素”时会发生什么?您是否成功地将“IsExpanded”属性设置为 false?但它实际上并没有折叠它们?
标签: c# xaml listview xamarin xamarin.forms