【问题标题】:CollectionView or ListView with buttons in each row for my shopping cartCollectionView 或 ListView 在我的购物车的每一行中都有按钮
【发布时间】:2021-05-04 21:36:27
【问题描述】:

我知道如何添加一个集合视图并用我的数据库中的数据填充它。我也知道如何在 collectionview 中选择一行并从不在我的 collectionview 中的按钮中删除该行(如页面底部的按钮) 但是我想做的是在每个单元格中创建一个删除/删除按钮,当单击相应的按钮时,我需要获取集合视图的选定索引并将其删除。

我需要的问题和帮助是如何为相应行中的每个按钮编写 buttonclick 事件。

请帮忙。

这是我的代码:

Customer.cs 命名空间测试 { 公开课客户 {
公共 int ID { 获取;放; } 公共字符串名称 { 获取;放; } } }

***MainPage.cs***
namespace TEST
{
    public partial class MainPage : ContentPage
    {

        public ObservableCollection<Customers>CustomerCollection;

        private int _itemId = 0;

        public MainPage()
        {
            InitializeComponent();

            CreateCustomerItems();
        }


        private void CreateCustomerItems()
        {

            CustomerCollection = new ObservableCollection<Customers>()
            {
                new Customers(){Id = 134, Name = "John Roberts"},
                new Customers(){Id = 145, Name = "Olyvia Johnson"},
                new Customers(){Id = 154, Name = "Paul Cippone"},
                new Customers(){Id = 163, Name = "Robert Jones"},
                new Customers(){Id = 171, Name = "Shannon Richards"},
                new Customers(){Id = 186, Name = "Tamica Alvirez"},
                new Customers(){Id = 195, Name = "Frank Bellford"},
                new Customers(){Id = 204, Name = "Kathryn Brown"},
            };

            CvTest.ItemsSource = CustomerCollection;
        
        }



        public void Delete(Object Sender, EventArgs args)
        {

            //THIS IS WHERE I AM LOST I CAN GET THE DATA BELOW WITH COLLECTION VIEW SELECTION CHANGED BUT HOW CAN I ACHIEVE THAT HERE? 
            var btn = (Button)Sender;
            //var item = (MainPage)btn.BindingContext;
            //item.

            CustomerCollection.Remove //THIS IS WHERE I AM LOST
        }

       

        private async void CvTest_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

            var currentSelection = e.CurrentSelection.FirstOrDefault() as Customers;
            if (currentSelection == null) return;

            _itemId = currentSelection.Id;
            await DisplayAlert("Testing", "You selected the CollectionView with the customer id of: " + _itemId.ToString(), "Continue");


        }

    }
}


***MainPage.xaml***
 <CollectionView x:Name="CvTest" SelectionMode="Single" SelectionChanged="CvTest_SelectionChanged">
        <CollectionView.ItemTemplate>
            <DataTemplate>
                <Grid  >

                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                        <RowDefinition Height="Auto" />
                    </Grid.RowDefinitions>

                    <Label Text="{Binding Id}" Grid.Column="0" Grid.Row="0" HorizontalOptions="Center"/>
                    <Label Text="{Binding Name}" Grid.Column="0" Grid.Row="1" HorizontalOptions="Center"/>

                    <Button Text="Delete" Grid.Column="0" Grid.Row="2" Clicked="Delete"/>


                </Grid>
            </DataTemplate>
        </CollectionView.ItemTemplate>
    </CollectionView>

【问题讨论】:

    标签: xamarin xamarin.forms


    【解决方案1】:

    在您的点击处理程序中,您可以使用ButtonBindingContext

    void ButtonClick(object sender, EventArgs a)
    {
        var btn = (Button)sender;
        // you will need to cast to the appropriate type of object here
        var item = (YourClassNameHere)btn.BindingContext;
    
        // you can now lookup item by ID or reference in your Collection
        // and delete/modify/etc it
    }
    

    针对您的具体示例

    public void Delete(Object Sender, EventArgs args)
        {
            var btn = (Button)Sender;
            var item = (Customer)btn.BindingContext;
    
            CustomerCollection.Remove(item);
        }
    

    【讨论】:

    • 这给了我一个错误:System.InvalidCastException Message=Specified cast is not valid。 // 你需要在这里转换成适当类型的对象是什么意思
    • 如果您的ItemsSourceList&lt;Widget&gt;,那么您将转换为Widget,因为列表的每一行代表Widget 的一个实例
    • 如何在按钮点击事件中检索同一集合视图行中被点击按钮的同一行的绑定数据?
    • 使用我提供的代码。这正是它的作用。它获取按钮所在行的绑定项。
    • 请编辑您的原始问题并在此处添加格式化代码。塞在 cmets 中的代码无法阅读。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-16
    相关资源
    最近更新 更多