如何在列表视图索引中切换子元素的可见性?
Posted
技术标签:
【中文标题】如何在列表视图索引中切换子元素的可见性?【英文标题】:How to toggle visibility of child elements in list view index? 【发布时间】:2021-06-15 20:26:54 【问题描述】:我正在使用 Xamarin.Forms 制作 UWP 应用程序。以下代码是 XAML 的 sn-p。此页面包含一个显示已解析 JSON 数据的列表视图。
<ListView x:Name="listView"
ItemTapped="ItemTapped"
Style="StaticResource openpageListView">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Frame Padding="0, 40, 0, 40">
<StackLayout HorizontalOptions="CenterAndExpand"
VerticalOptions="StartAndExpand">
<StackLayout Orientation="Horizontal"
HeightRequest="30"
WidthRequest="750"
HorizontalOptions="CenterAndExpand"
VerticalOptions="Start">
<Label Text="Topic: "
TextColor="White"
FontAttributes="Bold"
HorizontalOptions="StartAndExpand"/>
<Label Text="Binding Topic"
HorizontalOptions="StartAndExpand"/>
<Label Text="Input Type / Output Type: "
TextColor="White"
FontAttributes="Bold"
HorizontalOptions="EndAndExpand" />
<Label HorizontalOptions="EndAndExpand">
<Label.Text>
<MultiBinding StringFormat="0 => 1">
<Binding Path="InputType" />
<Binding Path="OutputType" />
</MultiBinding>
</Label.Text>
</Label>
</StackLayout>
<!-- Boolean specific subsection -->
<Grid x:Name="boolean"
IsVisible="false"
Padding="0, 15, 0, 15"
RowSpacing="15"
ColumnSpacing="15"
HorizontalOptions="CenterAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="205" />
<ColumnDefinition Width="205" />
<ColumnDefinition Width="205" />
</Grid.ColumnDefinitions>
<StackLayout Orientation="Horizontal" Grid.Column="0" Grid.Row="0">
<Label Text="Default: "
TextColor="White"
FontAttributes="Bold"/>
<Label Text="Binding Default" />
</StackLayout>
<StackLayout Orientation="Horizontal" Grid.Column="1" Grid.Row="0">
<Label Text="Invert: "
TextColor="White"
FontAttributes="Bold" />
<Label Text="Binding Invert" />
</StackLayout>
<StackLayout Orientation="Horizontal" Grid.Column="2" Grid.Row="0">
<Label Text="Button / Axis ID: "
TextColor="White"
FontAttributes="Bold" />
<Label Text="Binding ButtonId" />
</StackLayout>
</Grid>
<!-- Analog Specific Subsection -->
<StackLayout x:Name="analog"
WidthRequest="750"
IsVisible="true">
<Grid Padding="0, 15, 0, 15"
RowSpacing="15"
ColumnSpacing="50"
HorizontalOptions="CenterAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="250" />
<ColumnDefinition Width="250" />
</Grid.ColumnDefinitions>
<StackLayout Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
<Label Text="Button / Axis ID: "
TextColor="White"
FontAttributes="Bold" />
<Label Text="Binding AxisId" />
</StackLayout>
<StackLayout Orientation="Horizontal" Grid.Row="0" Grid.Column="1">
<Label Text="Clamp: "
TextColor="White"
FontAttributes="Bold" />
<Label Text="Binding Clamp" />
</StackLayout>
</Grid>
<Grid Padding="0, 0, 0, 15"
RowSpacing="15"
ColumnSpacing="50"
HorizontalOptions="CenterAndExpand">
<Grid.RowDefinitions>
<RowDefinition Height="30"/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="205" />
<ColumnDefinition Width="205" />
<ColumnDefinition Width="205" />
</Grid.ColumnDefinitions>
<StackLayout Orientation="Horizontal" Grid.Row="0" Grid.Column="0">
<Label Text="Default: "
TextColor="White"
FontAttributes="Bold" />
<Label Text="Binding Default" />
</StackLayout>
<StackLayout Orientation="Horizontal" Grid.Row="0" Grid.Column="1">
<Label Text="Scalar: "
TextColor="White"
FontAttributes="Bold" />
<Label Text="Binding Scalar" />
</StackLayout>
<StackLayout Orientation="Horizontal" Grid.Row="0" Grid.Column="2">
<Label Text="Bias: "
TextColor="White"
FontAttributes="Bold" />
<Label Text="Binding Bias" />
</StackLayout>
</Grid>
</StackLayout>
这就是“布尔”小节的样子。
这就是“模拟”小节的样子。
有没有办法在运行时循环遍历代码隐藏中 listView 的每一行并将布尔/模拟的 IsVisible 属性切换为 true?我想让它使某些行的布尔部分可见,而其他行的模拟部分可见。
【问题讨论】:
【参考方案1】:是的,有。但我不认为这是一个好主意。相反,您可以做的是将 ListView.DataTemplate 拆分为两个单独的文件并使用 DataTemplateSelector
查看这些链接了解更多详情:
Creating a Xamarin.Forms DataTemplate
Creating a Xamarin.Forms DataTemplateSelector
【讨论】:
以上是关于如何在列表视图索引中切换子元素的可见性?的主要内容,如果未能解决你的问题,请参考以下文章
如何在 UIStackView 中切换排列子视图的可见性,这在 tableViewCell swift3 中