在ListView的itemTapped事件中删除或添加ListView中的项目

时间:2016-07-08 10:37:53

标签: c# listview observablecollection

我有列表视图,其数据源是ObservableCollection,如下所示。

    ObservableCollection<empList_Model> diselectEmpModel = new ObservableCollection<empList_Model>();


    ObservableCollection<empList_Model> selectEmpModel = new ObservableCollection<empList_Model>();

        for (int i = 0; i < 10; i++) 
        selectEmpModel.Add (new empList_Model{ empName = "Employee " + i.ToString (), id = i.ToString () });
             selectEmpModel.Add (new empList_Model{ empName = "Select All ", id = "All" });
             listof_selectEmployee.ItemsSource = selectEmpModel;        

    public void  itemSelectedof_selectEmployeeList(object sender, SelectedItemChangedEventArgs e)
            {

                var listview = sender as ListView;
                var itemFor_AddingRemoving = listview.BindingContext as empList_Model;
                selectEmpModel.Remove(itemFor_AddingRemoving);  

                listof_selectEmployee.ItemsSource = selectEmpModel; 

                diselectEmpModel.Add (itemFor_AddingRemoving);
                listof_diselectEmployee.ItemsSource = diselectEmpModel;     


            }
public  class empList_Model
        {
            public string empName{ get; set;}
            public int Selected{ get; set;}
            public string id{ get; set;}
        }

Xaml:

<ListView  Grid.Column="0" Grid.Row="1" x:Name="listof_selectEmployee" ItemSelected="itemSelectedof_selectEmployeeList" BackgroundColor="Gray" RowHeight="40" SeparatorVisibility="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout  Padding="20">                                
                            <Label Text="{Binding empName}" VerticalOptions="CenterAndExpand" TextColor="{x:Static color:ColorResources.listTextColor}" />                               
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>


         <ListView  Grid.Column="1" Grid.Row="1" x:Name="listof_diselectEmployee" ItemSelected="itemSelectedof_diselectEmployeeList" BackgroundColor="Silver" RowHeight="40" SeparatorVisibility="None">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout  Padding="20">                                
                             <Label Text="{Binding empName}" VerticalOptions="CenterAndExpand" TextColor="{x:Static color:ColorResources.listTextColor}" />                               
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>

所以,我上面有两个ListViews,我希望从第一个列表中删除一个项目,并在用户点击第一个ListView(itemSelectedof_selectEmployeeList()事件)的任何项目时添加到第二个ListView。出于某种原因,当我点击ListView的任何项目时,没有任何反应。项目不会从第一个列表中删除,也不会添加到第二个列表中。我不知道我错过了什么。

0 个答案:

没有答案