在 Xamarin Form 中单击一行上的 RadioButton 时获取 ListView 项目的索引
Get index of ListView item when RadioButton on one row is clicked in Xamarin Form
我正在 ListView row
中实现 4 RadioButtons
并将它们与模型属性绑定。问题是当用户更改预先设置的 属性 并且 CheckedChanged event
触发时,我怎样才能知道用户更改了 属性 的 row(Index)
?
这是我对 ListView 的实现。
<ListView x:Name ="personlist" ItemsSource="{Binding PersonList}" ItemSelected="personlist_ItemSelected" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid RowDefinitions="*" ColumnDefinitions="3*,*,*,*,*" BackgroundColor="{Binding RowColor}">
<Label Text="{Binding Name}" Grid.Row="0" Grid.Column="0" VerticalTextAlignment="Center" FontSize="20" TextColor="Black"/>
<RadioButton Value="Travel" IsChecked="{Binding Travel, Mode=TwoWay}" Grid.Row="0" Grid.Column="1" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Value="Reading" IsChecked="{Binding Reading, Mode=TwoWay}" Grid.Row="0" Grid.Column="2" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Value="Sports" IsChecked="{Binding Sports, Mode=TwoWay}" Grid.Row="0" Grid.Column="3" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Value="Dance" IsChecked="{Binding Dance, Mode=TwoWay}" Grid.Row="0" Grid.Column="4" CheckedChanged="RadioButton_CheckedChanged"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在.xaml.cs
private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
// I want to get an index of the list item here.
}
有什么办法可以实现吗?这种方式或其他方式。
提前致谢!
使用BindingContext
private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
var button = (RadioButton)sender;
var item = (MyClassName)button.BindingContext;
}
item
将是绑定到 ListView
中该行的对象
我正在 ListView row
中实现 4 RadioButtons
并将它们与模型属性绑定。问题是当用户更改预先设置的 属性 并且 CheckedChanged event
触发时,我怎样才能知道用户更改了 属性 的 row(Index)
?
这是我对 ListView 的实现。
<ListView x:Name ="personlist" ItemsSource="{Binding PersonList}" ItemSelected="personlist_ItemSelected" SelectionMode="Single">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid RowDefinitions="*" ColumnDefinitions="3*,*,*,*,*" BackgroundColor="{Binding RowColor}">
<Label Text="{Binding Name}" Grid.Row="0" Grid.Column="0" VerticalTextAlignment="Center" FontSize="20" TextColor="Black"/>
<RadioButton Value="Travel" IsChecked="{Binding Travel, Mode=TwoWay}" Grid.Row="0" Grid.Column="1" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Value="Reading" IsChecked="{Binding Reading, Mode=TwoWay}" Grid.Row="0" Grid.Column="2" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Value="Sports" IsChecked="{Binding Sports, Mode=TwoWay}" Grid.Row="0" Grid.Column="3" CheckedChanged="RadioButton_CheckedChanged"/>
<RadioButton Value="Dance" IsChecked="{Binding Dance, Mode=TwoWay}" Grid.Row="0" Grid.Column="4" CheckedChanged="RadioButton_CheckedChanged"/>
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
在.xaml.cs
private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
// I want to get an index of the list item here.
}
有什么办法可以实现吗?这种方式或其他方式。
提前致谢!
使用BindingContext
private void RadioButton_CheckedChanged(object sender, CheckedChangedEventArgs e)
{
var button = (RadioButton)sender;
var item = (MyClassName)button.BindingContext;
}
item
将是绑定到 ListView