IsChecked属性绑定到可观察的集合

时间:2016-06-14 07:12:27

标签: wpf

我正在使用WPF的DataGrid。在这个网格中,第一列是复选框。从excel导出列。因为Itemsource是该数据表的默认视图。我的问题是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();
        }

1 个答案:

答案 0 :(得分:1)

我从您的问题中了解到,您有两个数据源ObservableCollection用于检查状态,DataTable用于显示从Excel导入的数据。

但是由于DataGrid只有一个属性DataContext,您需要将这两个数据源合并为一个,然后绑定这个新的数据源。

我看到的一个简单选项是您可以向DataTable添加一个布尔列,然后绑定DataTable。这样您就可以获得检查状态,并且还能够显示Excel数据。