简体   繁体   English

Xamarin.Forms - 如何使用 ObservableCollection 从包含 ListView 中选择项目

[英]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中,您依赖MyListSelectedItem属性。 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.

相关问题 如何使用 XAML 在 Xamarin.Forms 的 ListView 中选择一个项目 - How can I select an item in a ListView in Xamarin.Forms with XAML 将字典中的JSON API解析为Xamarin.Forms中的ListView的ObservableCollection - Parse JSON API from Dictionary to ObservableCollection for ListView in Xamarin.Forms Xamarin Forms - 单击图像时如何选择 ListView 项? - Xamarin Forms - How to select a ListView item when its image is clicked? 如何更改listview所选项目的文本颜色Xamarin.forms - How to change the listview selected item text color Xamarin.forms 如何在Xamarin.Forms中将CommandParameter设置为ListView项本身 - How to set the CommandParameter to the ListView item itself in Xamarin.Forms 如何在Xamarin.Forms中实现ListView的DataTemplate子项的方法 - How to implement method of sub item of DataTemplate of ListView in Xamarin.Forms 更新ObservableCollection之后,Xamarin.Forms ListView发送空事件 - After update of ObservableCollection Xamarin.Forms ListView sends null events 清除ListView项目选择Xamarin.Forms - clear ListView item selection Xamarin.Forms Xamarin.Forms ListView在Android上取消选择项目 - Xamarin.Forms ListView deselect item on Android 如何通过打开上下文操作菜单从ListView中获取所选项目? (Xamarin.Forms) - How do I get the selected item from a ListView by opening the context actions menu? (Xamarin.Forms)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM