我正在使用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();
}
請同時發佈您的Model和ViewModel類的代碼。專家可能需要研究它 –