<ListView Grid.ColumnSpan="2" Grid.Row="1" ItemsSource="{Binding TaskList}" SelectedItem="{Binding SelectedTask}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout Orientation="Horizontal" >
<Label Text="{Binding TaskName}" />
<StackLayout Padding="150,0,0,0">
<Switch></Switch>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
Hello, I am trying to get the selected item from a ListView
by using switch and could not be sure about how to handle.
答案 0 :(得分:1)
以下示例显示如何绑定数据以在ListView内部切换以及如何在切换事件上获取切换数据:
在xaml:
<ListView Grid.ColumnSpan="2" Grid.Row="1" ItemsSource="{Binding TaskList}" SelectedItem="{Binding SelectedTask}" >
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell >
<StackLayout Orientation="Horizontal" >
<Label Text="{Binding TaskName}" />
<StackLayout Padding="150,0,0,0">
<Switch x:Name="chooseItem" VerticalOptions="Center"
IsToggled="{Binding IsToggled, Mode=TwoWay}"
Toggled="Switch_Toggled" />
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在cs:
private void Switch_Toggled(object sender, ToggledEventArgs e)
{
try
{
var switchItem = (Switch)sender;
var selectedTask = (SelectedTask)switchItem.BindingContext;
//here you will get only toggled element model
}
catch (Exception ex)
{
}
}