如何在Listview中select只有一个Toggle Switch?
How to select only one Toggle Switch in Listview?
我有一个 Listview
有拨动开关。我只想 select Listview
中的一项带有切换开关。当我 select 第二次切换时,第一个切换必须再次停用。例如在图片中;当我 select Rekorida 然后我 select Merchandizing
时, Rekorida
必须返回禁用。每次只能激活一个选项。可以在 Xamarin 中实现吗?
我的 listView 代码:
<ScrollView>
<ListView x:Name="ShipListView" HasUnevenRows="True" SeparatorVisibility="Default" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="4, 3, 4, 3" Padding="2" BackgroundColor="White">
<Grid.RowDefinitions HeightRequest="40">
<RowDefinition></RowDefinition>
<!--<RowDefinition Height="20"></RowDefinition>-->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="40*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Text="{Binding ShipName}" TextColor="DeepPink" Grid.Column="0" FontAttributes="Bold"/>
<Switch IsToggled="{Binding Selected}" Grid.Column="2" Toggled="OnShipToggled" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollView>
还有我的函数
async void OnShipToggled(object sender, ToggledEventArgs e)
{
checkShipSelected = !checkShipSelected;
if(checkShipSelected)
{
ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
ParametersMemberShipInformation model = cell.BindingContext as ParametersMemberShipInformation;
if (model != null)
{
selectedMemberShipOid = model.Oid;
}
}
else
{
return;
}
}
我正在尝试在列表视图中获取 selected 项目的 id,我成功地做到了这一点。但我希望用户在同一时间只能 select 一项,因为视觉效果很好,不会混淆。
在你的代码隐藏文件中获取你的视图模型,然后过滤掉选定的切换并将其余的标记为 false
private YourViewModel MyViewModel { get => BindingContext as YourViewModel; }
if (model != null)
{
selectedMemberShipOid = model.Oid;
MyViewModel.ShipListView.Where(x=> x.Oid !=
selectedMemberShipOid).Foreach(x=> x.Selected = false)
}
我有一个 Listview
有拨动开关。我只想 select Listview
中的一项带有切换开关。当我 select 第二次切换时,第一个切换必须再次停用。例如在图片中;当我 select Rekorida 然后我 select Merchandizing
时, Rekorida
必须返回禁用。每次只能激活一个选项。可以在 Xamarin 中实现吗?
我的 listView 代码:
<ScrollView>
<ListView x:Name="ShipListView" HasUnevenRows="True" SeparatorVisibility="Default" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="4, 3, 4, 3" Padding="2" BackgroundColor="White">
<Grid.RowDefinitions HeightRequest="40">
<RowDefinition></RowDefinition>
<!--<RowDefinition Height="20"></RowDefinition>-->
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="80*"></ColumnDefinition>
<ColumnDefinition Width="*"></ColumnDefinition>
<ColumnDefinition Width="40*"></ColumnDefinition>
</Grid.ColumnDefinitions>
<Label Text="{Binding ShipName}" TextColor="DeepPink" Grid.Column="0" FontAttributes="Bold"/>
<Switch IsToggled="{Binding Selected}" Grid.Column="2" Toggled="OnShipToggled" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ScrollView>
还有我的函数
async void OnShipToggled(object sender, ToggledEventArgs e)
{
checkShipSelected = !checkShipSelected;
if(checkShipSelected)
{
ViewCell cell = (sender as Switch).Parent.Parent as ViewCell;
ParametersMemberShipInformation model = cell.BindingContext as ParametersMemberShipInformation;
if (model != null)
{
selectedMemberShipOid = model.Oid;
}
}
else
{
return;
}
}
我正在尝试在列表视图中获取 selected 项目的 id,我成功地做到了这一点。但我希望用户在同一时间只能 select 一项,因为视觉效果很好,不会混淆。
在你的代码隐藏文件中获取你的视图模型,然后过滤掉选定的切换并将其余的标记为 false
private YourViewModel MyViewModel { get => BindingContext as YourViewModel; }
if (model != null)
{
selectedMemberShipOid = model.Oid;
MyViewModel.ShipListView.Where(x=> x.Oid !=
selectedMemberShipOid).Foreach(x=> x.Selected = false)
}