如何在c#WPF中从Listview中获取所有选中的选定项目

时间:2017-09-06 14:30:10

标签: c# wpf listview checkbox

我想获取复选框已选中的列表视图中的所有项目。

lvwStudent

      <ListView  x:Name="lvwStudent"  IsSynchronizedWithCurrentItem="True"  SelectionMode="Multiple" HorizontalAlignment="Stretch" HorizontalContentAlignment="Center" VerticalContentAlignment="Center">
        <ListView.View>
            <GridView>
                <GridViewColumn  Width="50">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <CheckBox x:Name="cboxSelected" Content ="{Binding ID}"   Width="20" Height="20" BorderBrush="#FF0C6161" HorizontalAlignment="Center" HorizontalContentAlignment="Center" Tag="{Binding ID}" IsChecked="{Binding IsChecked}" Checked="cboxSelected_Checked_1" />
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="NO." Width="53">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate >
                            <Label Content ="{Binding ID}" FontSize="14" HorizontalContentAlignment="Center"></Label>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
                <GridViewColumn Header="STUDENT NAME" Width="auto">
                    <GridViewColumn.CellTemplate>
                        <DataTemplate>
                            <Label Content ="{Binding STUDENT_NAME}" FontSize="14" HorizontalContentAlignment="Center"></Label>
                        </DataTemplate>
                    </GridViewColumn.CellTemplate>
                </GridViewColumn>
            </GridView>
        </ListView.View>
    </ListView>

当我单击选择按钮时,我想获取所有选定的ID并存储在数组中。

    private void btnSelect_Click(object sender, RoutedEventArgs e)
    {
       int[] selectedId;

        if(lvwMachine.SelectedItems.Count > 0)
        {

            foreach(..... )
            {
              //add all selected id in array selectedId


            }
         }
List<Student>StudentList = new Students().getStudent();
List<ListViewItem> ITEM = new List<ListViewItem>(); 

foreach (var s in StudentList) 
{ 
       ListViewItem OneItem = new ListViewItem(); 
       OneItem.Content = new Student() 
           {ID = s.ID, STUDENT_NAME = s.name}; 

       ITEM.Add(OneItem); 
       lvwMachine.ItemsSource = ITEM; 
} 

1 个答案:

答案 0 :(得分:0)

你可以试试这个:

var app = angular.module('app', ['chart.js']);

app.controller("LineCtrl", function($scope) {
   $scope.labels = ["2017-03-09", "2017-03-10", "2017-03-11", "2017-03-12", "2017-03-13", "2017-03-14"];
   $scope.colors = ['#07C'];
   $scope.data = [
      [3, 2, 5, 1, 4, 2]
   ];
   $scope.options = {
      legend: {
         display: true
      },
      scales: {
         xAxes: [{
            type: 'time'
         }]
      },
      annotation: {
         annotations: [{
            type: 'line',
            mode: 'vertical',
            scaleID: 'x-axis-0',
            value: '2017-03-11',
            borderColor: 'red',
            borderWidth: 2
         }]
      }
   }
   $scope.datasetOverride = [{
      label: 'TIME',
      fill: false
   }];
});