2016-06-14 95 views
0

我正在使用WPF的DataGrid。在這個網格中,第一列是複選框。剩餘列從Excel中導出。因此,Itemsource是該數據表的默認視圖。我的問題是IsChecked屬性綁定到可觀察集合。我無法保存複選框列的選中狀態請找到下面的代碼。IsChecked屬性綁定到可觀察集合

<DataGrid x:Name="grdSelect" HorizontalAlignment="Left" Margin="10,20,0,0" VerticalAlignment="Top" 
        MinWidth="186" CanUserSortColumns="True" IsReadOnly="True" Grid.Column="1" 
        ItemsSource="{Binding}" CanUserResizeColumns="False" 
        CanUserAddRows="False" CanUserReorderColumns="False" 
        MaxHeight="300" SelectionMode="Extended" SelectionChanged="DataGridSelectionChangedEventHandler"> 
        <DataGrid.Resources> 
         <Style TargetType="{x:Type DataGridColumnHeader}"> 
          <Setter Property="Background" Value="#FF003878" /> 
          <Setter Property="Foreground" Value="#FFF0F0F1" /> 
          <Setter Property="Width" Value="Auto" /> 
         </Style> 
        </DataGrid.Resources> 
        <DataGrid.Columns> 
         <DataGridTemplateColumn x:Name="checkboxtemplate"> 
          <DataGridTemplateColumn.CellTemplate> 
           <DataTemplate> 
            <CheckBox x:Name="checkData" IsChecked="{Binding DataChecked, 
             UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" 
             Width="30" HorizontalAlignment="Center" 
               Unchecked="DataRowCheckedUncheckedEventHandler" 
             Checked="DataRowCheckedUncheckedEventHandler"/> 
           </DataTemplate> 
          </DataGridTemplateColumn.CellTemplate> 
         </DataGridTemplateColumn> 
        </DataGrid.Columns> 
       </DataGrid> 

if (!string.IsNullOrEmpty(dataPath)) 
      { 
       Microsoft.Office.Interop.Excel.Application excelApp = new Microsoft.Office.Interop.Excel.Application(); 
       //Dynamic File Using Uploader........... 
       Microsoft.Office.Interop.Excel.Workbook excelBook = 
        excelApp.Workbooks.Open(dataPath, 0, true, 5, "", "", true, Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "\t", false, false, 0, true, 1, 0); 
       Microsoft.Office.Interop.Excel.Worksheet excelSheet = 
        (Microsoft.Office.Interop.Excel.Worksheet)excelBook.Worksheets.get_Item(1); ; 
       Microsoft.Office.Interop.Excel.Range excelRange = excelSheet.UsedRange; 

      string strCellData = ""; 
      double douCellData; 
      int rowCnt = 0; 
      int colCnt = 0; 

      DataTable dt = new DataTable(); 
      for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++) 
      { 
       string strColumn = ""; 
       strColumn = (string)(excelRange.Cells[1, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2; 
       dt.Columns.Add(strColumn, typeof(string)); 
      } 

      for (rowCnt = 2; rowCnt <= excelRange.Rows.Count; rowCnt++) 
      { 
       string strData = ""; 
       for (colCnt = 1; colCnt <= excelRange.Columns.Count; colCnt++) 
       { 
        try 
        { 
         strCellData = (string)(excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2; 
         if (strCellData != null) 
         { 
          strData += strCellData + "|"; 
         } 
        } 
        catch (Exception ex) 
        { 
         douCellData = (excelRange.Cells[rowCnt, colCnt] as Microsoft.Office.Interop.Excel.Range).Value2; 
         strData += douCellData.ToString() + "|"; 
        } 
       } 
       if (!string.IsNullOrEmpty(strData)) 
       { 
        strData = strData.Remove(strData.Length - 1, 1); 
        dt.Rows.Add(strData.Split('|')); 
       } 
      } 

      grdSelect.ItemsSource = dt.DefaultView;     
      //grdSelect.Loaded += SetMinWidths; 
      //grdSelect.Width = excelRange.Rows.Count * 27; 

      excelBook.Close(true, null, null); 
      excelApp.Quit(); 
     } 
+0

請同時發佈您的Model和ViewModel類的代碼。專家可能需要研究它 –

回答

1

我從你的問題明白你的檢查狀態和DataTable用於顯示從Excel導入數據的兩個數據源的ObservableCollection。

但是由於DataGrid只有一個屬性DataContext,因此需要將這兩個數據源合併爲一個,然後綁定這個新的數據源。

我看到的一個簡單的選擇是,您可以添加一個更多的布爾列到您的DataTable,然後綁定DataTable。這樣你可以得到檢查狀態,並且能夠顯示excel數據。

+0

你可以請分享的代碼 –