简体   繁体   English

列表框中的复选框以选择多个项目,并使用MVVM将其添加到另一个列表框中

[英]Checkbox in Listbox to select multiple items and add it to another listbox using MVVM

Using MVVM, 使用MVVM,

I have two listboxes which contains Checkboxes and data is binded from database. 我有两个包含复选框的列表框,数据是从数据库绑定的。

The Items which are checked in first Listbox want to add it in Second listbox. 在第一个列表框中选中的项目要在第二个列表框中添加。

First ListBox: 第一个列表框:

 <pmControls:pmListBox SelectionMode="Multiple" Grid.Row="1" Margin="3" ItemsSource="{Binding ParcelFacilities}" >

            <interactivity:Interaction.Triggers>
                <interactivity:EventTrigger EventName="SelectionChanged">
                    <shared:EventToCommandTrigger Command="{Binding Listbox_SelectionChangeCommand}" />
                </interactivity:EventTrigger>
            </interactivity:Interaction.Triggers>


            <pmControls:pmListBox.ItemTemplate >
                <DataTemplate >
                    <pmControls:pmCheckBox  Content="{Binding Title}" Margin="3" Width="200"  IsChecked="{Binding checkedParcelFacility}" >

                    </pmControls:pmCheckBox>

                </DataTemplate>
            </pmControls:pmListBox.ItemTemplate>

Second ListBox: 第二个ListBox:

 <pmControls:pmListBox SelectionMode="Multiple"   Grid.Row="1" Margin="3" ItemsSource="{Binding Selected_ParcelFacilities}" 
                                   Height="100">

            <pmControls:pmListBox.ItemTemplate >
                <DataTemplate >

                    <pmControls:pmCheckBox Content="{Binding Title}" Margin="3" Width="200" ></pmControls:pmCheckBox>

                </DataTemplate>
            </pmControls:pmListBox.ItemTemplate>

In ViewMOdel: 在ViewMOdel中:

I have handled SelectionChanged Event For first Listbox and tryied to add checked element to the collection Named as Selected_ParcelFacilities and Binded it to Second Listbox. 我已经为第一个列表框处理了SelectionChanged事件,并尝试将选中的元素添加到名为Selected_ParcelFacilities的集合中并将其绑定到第二个列表框。

         public ParcelViewModel(IModalDialogService modalDialogService, IMessageBoxService messageBoxService)
     {       

            parcelFacilities = new ObservableCollection<Parcel_Facility>();

            Selected_ParcelFacilities = new ObservableCollection<Parcel_Facility>();

            Selected_ParcelFacilities.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler(Selected_ParcelFacilities_CollectionChanged);
  }


 void Selected_ParcelFacilities_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
 {
     OnPropertyChanged("Selected_ParcelFacilities");
 }

private void Executelistbox_SelectionChangeCommand(EventToCommandArgs args)
 {

     bool a = checkedParcelFacility;

     foreach (Parcel_Facility item in parcelFacilities)
     {

                if (Selected_ParcelFacilities != null)
                {
                    Selected_ParcelFacilities.Add(item);
                }                    
     }             
 }

But using above code all items from first listbox are adding to second , i am not getting how to check wheather its cheked or not. 但是使用上面的代码,第一个列表框中的所有项都添加到第二个列表中,我不知道如何检查是否被选中。

Please Help. 请帮忙。

you can simply bind your second list box to SelectedItems of your first one. 您只需将第二个列表框绑定到第一个列表框的SelectedItems即可。 this would work with real selection in listbox first. 这将首先与列表框中的实际选择一起使用。

<ListBox x:Name="second" ItemsSource="{Binding Elementname=first, Path=SelectedItems, Mode=OneWay}"/>

another approach is to use a ICollectionView with filter for your second listbox. 另一种方法是对第二个列表框使用带有过滤器的ICollectionView。 the filter simply handle the checkedParcelFacility Property and the second listbox is bind to the ICollectionView. 过滤器只处理checkedParcelFacility属性,第二个列表框绑定到ICollectionView。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM