Listview不包含selectedItems的定义

时间:2019-03-28 07:26:00

标签: c# listview xamarin.forms

我正在使用Xamarin.Forms。我想在单击“删除”按钮后在列表视图中删除所选项目。

我的xaml

 <ListView x:Name="ProductsListView"
          HasUnevenRows="True"
          BackgroundColor="#ecf0f1"
          SeparatorVisibility="None"
          HorizontalOptions="FillAndExpand"
          VerticalOptions="FillAndExpand">

                <ListView.ItemTemplate>
                    <DataTemplate>
                        <ViewCell>
                            <StackLayout Margin="6,4,6,4"
                             BackgroundColor="White">
                                <StackLayout Orientation="Horizontal">
                                    <Label Text="Item ID" Margin="25,10,4,4" FontSize="Small" TextColor="Black"  />
                                    <Label Text="{Binding retail_modified_item_id}"  Margin="25,10,4,4" TextColor="Black" FontSize="12" />
                                </StackLayout>

                                <StackLayout Orientation="Horizontal">
                                    <Label Text="Name" Margin="25,2,8,4" TextColor="Black" FontSize="Small" />
                                    <Label Text="{Binding name}" Margin="32,1,8,4" TextColor="Black" FontSize="Small" />
                                    <Switch IsToggled="false"  Margin="210,2,2,2" Toggled="Switch_Toggled" />
                                </StackLayout>

                                <StackLayout Orientation="Horizontal">
                                    <Label Text="OldPrice"  Margin="25,2,8,4" TextColor="Black" FontSize="Small"/>
                                    <Label Text="{Binding old_price}" Margin="32,1,8,4" TextColor="Black" FontSize="Small" />
                                </StackLayout>

                                <StackLayout Orientation="Horizontal">
                                    <Label Text="NewPrice" Margin="25,2,8,4" TextColor="Black" FontSize="Small" />
                                    <Label Text="{Binding new_price}" Margin="32,1,8,4" TextColor="Black" FontSize="Small" />
                                </StackLayout>

                            </StackLayout>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>

我的CS代码

private void reject(object sender, EventArgs args)
    {
        foreach (var v in ProductsListView.SelectedItems)
        {
           ProductsListView.ItemSelected.Remove(v);
        }
        DisplayAlert("Rejected","Request Rejected!!", "OK");
    }

我收到此错误:

  

listview不包含selecteditem ItemSelected的定义

2 个答案:

答案 0 :(得分:2)

您可以尝试将其投射到ProductListView,

var selectedItems = (ListView/* or ProductListView*/)sender; //-> you need casting to access it.
  

您可以更改列表视图项的来源

public void reject(your_list_of_model_type your_list_of_model)
{
   your_list_of_model.RemoveRange(selectedItems);
   ProductListView.ItemSource = your_list_of_model;
}
  

解决方案2:

这是您可以做的:

这是我的模型课:

public class Item  
{  
   public string ItemName { get; set; }  
   public string ItemDetails { get; set; }  
}  

在我的XAML中,或者您也可以用代码编写此代码,将其绑定到Item模板的Command Parameter:

<Button Text="Delete" CommandParameter="{Binding ItemName}" Clicked="DeleteClicked"></Button>
Full Item Template will be like below :

<ListView.ItemTemplate>  
            <DataTemplate>  
               <ViewCell>  
                  <ViewCell.View>  
                     <StackLayout Orientation="Horizontal">  
                        <Label Text="{Binding ItemName}" HorizontalOptions="StartAndExpand" FontSize="30"></Label>  
                        <Button Text="Delete" CommandParameter="{Binding ItemName}" Clicked="DeleteClicked">        
                        </Button>  
                     </StackLayout>  
                  </ViewCell.View>  
               </ViewCell>  
            </DataTemplate>  
         </ListView.ItemTemplate>    

在代码文件中,您可以执行以下操作:

public void DeleteClicked(object sender, EventArgs e)  
{  
   var item = (Xamarin.Forms.Button)sender;  
   Item listitem = (from itm in allItems 
                    where itm.ItemName == item.CommandParameter.ToString() 
                    select itm)
                   .FirstOrDefault<Item>();  
   allItems.Remove(listitem);  
}  

重要提示:这只会从绑定的集合中删除该项目。要将其从原始列表中删除,您需要使用ObservableCollection

答案 1 :(得分:0)

您可以将所有已切换的项目添加到列表中,假设selectedItems。然后,当您单击REJECT按钮时,请从列表视图的数据源中删除selectedItems

完整代码如下:

MainPage.xaml:

<StackLayout Orientation="Vertical">
        <!-- Place new controls here -->
        <Button Text="APPROVE" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand" />
        <Button Text="REJECT" 
           HorizontalOptions="Center"
           VerticalOptions="CenterAndExpand"
                Clicked="reject"/>
        <ListView x:Name="ProductsListView" 
          HasUnevenRows="True"
          BackgroundColor="#ecf0f1"
          SeparatorVisibility="None"
          HorizontalOptions="FillAndExpand"
          VerticalOptions="FillAndExpand">

            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Orientation="Vertical" Margin="6,4,6,4"
                             BackgroundColor="White">

                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding name}" Margin="32,1,8,4" TextColor="Black" FontSize="Small" />
                                <Switch IsToggled="false"  Margin="210,2,2,2" Toggled="Switch_Toggled" />
                            </StackLayout>

                            <StackLayout Orientation="Horizontal">
                                <Label Text="{Binding retail_modified_item_id}"  Margin="25,10,4,4" TextColor="Black" FontSize="12" />
                            </StackLayout>

                            <StackLayout Orientation="Horizontal">
                                <Label Text="OldPrice"  Margin="25,2,8,4" TextColor="Black" FontSize="Small"/>
                                <Label Text="{Binding old_price}" Margin="32,1,8,4" TextColor="Black" FontSize="Small" />
                            </StackLayout>

                            <StackLayout Orientation="Horizontal">
                                <Label Text="NewPrice" Margin="25,2,8,4" TextColor="Black" FontSize="Small" />
                                <Label Text="{Binding new_price}" Margin="32,1,8,4" TextColor="Black" FontSize="Small" />
                            </StackLayout>

                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>

MainPage.xaml.cs:

public partial class MainPage : ContentPage
    {
        ObservableCollection<ItemModel> allItems = new ObservableCollection<ItemModel>();
        List<ItemModel> selectedItems = new List<ItemModel>();

        public MainPage()
        {
            InitializeComponent();
            InitializeData();
            ProductsListView.ItemsSource = allItems;
        }

        private void reject(object sender, EventArgs e)
        {
            foreach (var v in selectedItems)
            {
                allItems.Remove(v);
            }
            DisplayAlert("Rejected", "Request Rejected!!", "OK");
        }

        private void Switch_Toggled(object sender, ToggledEventArgs e)
        {
            var switch1 = (Switch)sender;
            var item = (ItemModel)switch1.BindingContext;

            var isSelected = !item.selected;

            if (isSelected)
            {
                selectedItems.Add(item);
            }
            else
            {
                selectedItems.Remove(item);
            }
        }

        private void InitializeData() {
            allItems.Add(new ItemModel { name = "Onion Rings, Medium",
                retail_modified_item_id = 1000630,
                old_price = 1.29,
                new_price = 9.45,
                selected = false
            });

            allItems.Add(new ItemModel
            {
                name = "Hashbrowns",
                retail_modified_item_id = 1000739,
                old_price = 0.99,
                new_price = 8.5,
                selected = false
            });

            allItems.Add(new ItemModel
            {
                name = "Amstel Light, Single",
                retail_modified_item_id = 1002038,
                old_price = 3.5,
                new_price = 18,
                selected = false
            });

        }
    }

ItemModel.cs:

class ItemModel
    {
        public string name { get; set; }
        public int retail_modified_item_id { get; set; }
        public double old_price { get; set; }
        public double new_price { get; set; }
        public bool selected { get; set; }
    }