I have an ListView
bound to an ObservableCollection<ItemID>
as shown. When I click a Delete button it calls DeleteButton_Clicked
correctly but then cannot access the objects. I need to delete object and then change the Scan.Text
property. Thank you for any help.
The ItemID
class is defined here:
public class ItemID
{
public string Name { get; set; }
public string Age { get; set; }
public double Value { get; set; }
}
This is code that's giving me trouble:
private void DeleteButton_Clicked(object sender, EventArgs e)
{
ItemID items = MyList.SelectedItem as ItemID;
Items.Remove(items);
MyList.SelectedItem = null;
Scan.Text = items.Value.ToString(); // it doesn't work
}
The Scan button creates an object and also changes the Scan.Text:
private async void ButtonScanDefault_Clicked(object sender, EventArgs e)
{
ZXingScannerPage scanPage;
scanPage = new ZXingScannerPage();
scanPage.OnScanResult += (result) =>
{
scanPage.IsScanning = false;
Device.BeginInvokeOnMainThread(async () =>
{
await Navigation.PopModalAsync();
await DisplayAlert("Scanned Barcode", result.Text, "OK");
Scan.Text = SumCost(Cost_Product(result.Text));
var item = new ItemID()
{
Name = "Name_ID:" + " " + result.Text,
Age = "Age:" + " " + Cost_Product(result.Text),
Value = Cost_Product(result.Text)
};
Items.Add(item);
});
};
await Navigation.PushModalAsync(scanPage);
}
XAML
<ListView Grid.Row="1"
ItemsSource="{Binding Items}"
HasUnevenRows="True"
x:Name="MyList">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Grid Margin="10">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Text="{Binding Name}" />
<Button Clicked="DeleteButton_Clicked"
Grid.Column="1"
Padding="30,10"
Text="Delete"
HorizontalOptions="EndAndExpand"
BackgroundColor="Black"
TextColor="White" />
</Grid>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>