[英]Xamarin.Forms - How to select item from its containing ListView with ObservableCollection
I have an ListView
bound to an ObservableCollection<ItemID>
as shown.如图所示,我有一个绑定到
ObservableCollection<ItemID>
的ListView
。 When I click a Delete button it calls DeleteButton_Clicked
correctly but then cannot access the objects.当我单击删除按钮时,它会正确调用
DeleteButton_Clicked
,但随后无法访问对象。 I need to delete object and then change the Scan.Text
property.我需要删除对象,然后更改
Scan.Text
属性。 Thank you for any help.感谢您的任何帮助。
The ItemID
class is defined here: ItemID
类在此处定义:
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: Scan 按钮创建一个对象并更改 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 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>
Here's what could be the problem: In your DeleteButton_Clicked
you rely on the SelectedItem
property of MyList
.这可能是问题所在:在您的
DeleteButton_Clicked
中,您依赖MyList
的SelectedItem
属性。 However, clicking the Delete button does not automatically select that list item.但是,单击“删除”按钮不会自动选择该列表项。 The way to determine the correct item to delete is to look at the
BindingContext
property of the button that sent the delete click, and cast this object to ItemID
.确定要删除的正确项目的方法是查看发送删除单击的按钮的
BindingContext
属性,并将此对象转换为ItemID
。
private void DeleteButton_Clicked(object sender, EventArgs e)
{
// The sender is the button you clicked
Button button = (Button)sender;
// The binding context of the button IS the
// item associated with the button.
ItemID item = (ItemID)button.BindingContext;
Items.Remove(item);
// Now you can adjust the Scan.Text however you want.
// THIS IS JUST AN EXAMPLE
ItemID defaultScanItem = Items.FirstOrDefault();
if (defaultScanItem == null)
{
Scan.Text = $"[Deleted {item.Name}]";
}
else
{
Scan.Text = $"[Deleted {item.Name}] Scan {defaultScanItem.Name}";
}
}
Before and after delete删除前后
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.