【问题标题】:IsChecked property is binded to an observable collectionIsChecked 属性绑定到可观察集合
【发布时间】:2016-06-14 07:12:27
【问题描述】:

我正在使用 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();
        }

【问题讨论】:

  • 请同时发布您的 Model 和 ViewModel 类的代码。专家可能需要调查一下

标签: wpf


【解决方案1】:

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

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

我看到的一个简单选项是,您可以在 DataTable 中再添加一个布尔列,然后绑定 DataTable。这样你就可以得到检查状态,也可以显示excel数据。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-22
    • 2020-06-05
    • 1970-01-01
    • 2012-06-30
    • 2013-05-21
    • 1970-01-01
    相关资源
    最近更新 更多