在WPF中基于DEV的复选框勾选一条或多条数据

<Window x:Class="WpfChart.Window4"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         Title="Window4" Height="550" Width="955" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid">
     <Grid>
         <dxg:GridControl  AutoGenerateColumns="AddNew" EnableSmartColumnsGeneration="True" HorizontalAlignment="Left" Margin="10,59,0,0" Name="gridControl1" VerticalAlignment="Top" Height="445" Width="738" >
             <dxg:GridControl.Columns >
                 <dxg:GridColumn FieldName="IsValid" Header="选中"  VisibleIndex="0" AllowEditing="False" AllowSorting="False" >
                     <dxg:GridColumn.CellTemplate>
                         <DataTemplate>
                             <CheckBox IsChecked="{Binding Data.IsValid}"
                                     HorizontalAlignment="Center" VerticalAlignment="Center" >
                             </CheckBox>
                         </DataTemplate>
                     </dxg:GridColumn.CellTemplate>
                 </dxg:GridColumn>
                 <dxg:GridColumn FieldName="ID" VisibleIndex="1" />
                 <dxg:GridColumn FieldName="Name" VisibleIndex="2" />
                 <dxg:GridColumn FieldName="StartDateTime" VisibleIndex="3" />
                 <dxg:GridColumn FieldName="EndDateTime" VisibleIndex="4" />
             </dxg:GridControl.Columns>
             <dxg:GridControl.View>
                 <dxg:TableView  ShowGroupPanel="False" AllowPerPixelScrolling="True" Name="tableView1" ShowTotalSummary="True" AllowFixedGroups="True" AllowColumnFiltering="False" AllowSorting="False" ShowFixedTotalSummary="True" />
             </dxg:GridControl.View>
         </dxg:GridControl>
         <Button Content="全选" Height="23" HorizontalAlignment="Left" Margin="16,28,0,0" Name="button1" VerticalAlignment="Top" Width="75" Click="button1_Click" />
         <Button Content="选中行信息" Height="23" HorizontalAlignment="Left" Margin="110,28,0,0" Name="button2" VerticalAlignment="Top" Width="75" Click="button2_Click" />
     </Grid>
 </Window>


 namespace WpfChart
 {
     /// <summary>
     /// Window4.xaml 的交互逻辑
    /// </summary>
     public partial class Window4 : Window
     {

        List<Project> data = null;


         public Window4()
         {
             InitializeComponent();
             data = new List<Project>();
             data.Add(new Project(1, "Apple", DateTime.Now, DateTime.Now, true));
             data.Add(new Project(2, "banana", DateTime.Now, DateTime.Now, false));
             data.Add(new Project(3, "orange", DateTime.Now, DateTime.Now, true));
             data.Add(new Project(4, "peach", DateTime.Now, DateTime.Now, false));
             data.Add(new Project(5, "pineapple", DateTime.Now, DateTime.Now, true));
             gridControl1.ItemsSource = data;
             DataContext = data;
             
         }

       

         /// <summary>
         /// 默认全选为false
         /// </summary>
         bool check = false;


         /// <summary>
         /// 全选及反选
        /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void button1_Click(object sender, RoutedEventArgs e)
         {
             bool newSelect = !check;
             check = !check;
             for (int i = 0; i < data.ToList().Count; i++)
             {
                 int rowHandle = this.gridControl1.GetRowHandleByListIndex(i);
                 gridControl1.SetCellValue(rowHandle, "IsValid", newSelect);
             }
         }

         /// <summary>
         /// 获取选中行的信息
        /// </summary>
         /// <param name="sender"></param>
         /// <param name="e"></param>
         private void button2_Click(object sender, RoutedEventArgs e)
         {
             string name = "";
             for (int i = 0; i < data.ToList().Count; i++)
             {
                 int rowHandle = this.gridControl1.GetRowHandleByListIndex(i);
                 object wholeRowObject = gridControl1.GetRow(rowHandle);
                 object rowCheck = gridControl1.GetCellValue(rowHandle, "IsValid");
                 bool ifCheck = (bool)rowCheck;
                 if (ifCheck)
                 {
                     object rowname = gridControl1.GetCellValue(rowHandle, "Name");
                     name += rowname.ToString() + ",";
                 }
             }
             MessageBox.Show(name);
         }


       
        
     }

     public class Project
     {
         static int num = 0;
         public int ID { get; set; }
         public string Name { get; set; }
         public DateTime StartDateTime { get; set; }
         public DateTime EndDateTime { get; set; }
         public bool IsValid { get; set; }
         public Project(int id, string name, DateTime startDateTime, DateTime endDateTime ,bool isvalid)
         {
             ID = id;
             Name = name;
             StartDateTime = startDateTime;
             EndDateTime = endDateTime;
             IsValid = isvalid;
         }
     }
 }


你可能感兴趣的:(在WPF中基于DEV的复选框勾选一条或多条数据)