I have a CollectionView
and each item contains 2 buttons. Edit and delete. I want to delete or edit the item when the button is pressed. But the problem is how can I get that particular item's corresponding object when the inside button is pressed? Because the selection mode is set to none, and pressing the button won't select the item either. So I can't access the item using by following -
(Model)collView.SelectedItem.Constructor.... (//Do something where x:Name = collView)
.
Here is something I tried -
<CollectionView x:Name="RequestCollectionView"
SelectionMode="None"
Margin="0,35,0,0">
<CollectionView.ItemTemplate>
<DataTemplate x:DataType="users:Student">
<StackLayout>
<Label x:Name="studentName"
Text="{Binding Name}"
Margin="0,25,0,0"/>
<Button x:Name="acceptBtn"
Clicked="AcceptBtn_Clicked"
HeightRequest="24"
WidthRequest="24"/>
<Button x:Name="rejectBtn"
Clicked="RejectBtn_Clicked"
HeightRequest="24"
WidthRequest="24"
Margin="0,0,15,0"/>
</StackLayout>
</DataTemplate>
</CollectionView.ItemTemplate>
</CollectionView>
I tried the following approch but it didn't work ->
private void AcceptBtn_Clicked(object sender, EventArgs e)
{
// Here I get NullRef Exception
testText.Text =
((Student)RequestCollectionView.SelectedItem).Name;
// var t = (Student)sender; //Invalid Cast
}
If the title is not appropiate, suggest or edit please. I couldn't find the appropiate title for this question. Pardon me.
use the BindingContext
private void AcceptBtn_Clicked(object sender, EventArgs e)
{
var btn = (Button) sender;
var student = (Student)btn.BindingContext;
testText.Text = student.Name;
}