DataGrid未填充

时间:2018-07-27 14:34:19

标签: c# wpf data-binding datagrid

我正在尝试在数据网格中显示数据,但未显示任何数据。通过断点时,我了解了以下内容:

  • 对象,收据中有数据
  • ocReceiptItem具有值
  • 将ocReceiptItem分配给grdSalesCart的数据上下文之后,我可以看到这些值位于数据上下文中
  • 绑定名称与ocReceiptItem中的名称匹配

我不确定为什么它没有填充,因为它在DataContext中有数据

DataGrid:

<DataGrid Name="grdSalesCart" Grid.Column="1" Grid.ColumnSpan="12" Grid.Row="12" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" 
                  AutoGenerateColumns="False" CanUserSortColumns="false" ScrollViewer.VerticalScrollBarVisibility="Auto" CanUserAddRows="False">
            <DataGrid.Columns>
                <DataGridTemplateColumn Header="Remove Item" Width="3*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Name="btnRemoveItem" Content="Remove Item" Click="btnRemoveItem_Click"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTemplateColumn Header="Edit Item" Width="2*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <Button Name="btnEditItem" Content="Edit Item" Click="btnEditItem_Click"/>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
                <DataGridTextColumn Header="SKU" Width="2*" Binding="{Binding intItemIndicator, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsReadOnly="True"/>
                <DataGridTextColumn x:Name="txtCartQuantity" Header="Quantity" Width="3*" Binding="{Binding intQuantity, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsReadOnly="True"/>
                <DataGridTextColumn Header="Description" Width="9*" Binding="{Binding varItemDescription, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsReadOnly="True"/>
                <DataGridTextColumn Header="Price" Width="3*" Binding="{Binding intItemPrice, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay, Converter={StaticResource Double}}" IsReadOnly="True"/>
                <DataGridTemplateColumn Header="Discount" Width="3*">
                    <DataGridTemplateColumn.CellTemplate>
                        <DataTemplate>
                            <StackPanel>
                                <TextBox Name="txtDiscountAmountCart" Text="{Binding intItemDiscount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsEnabled="False"/>
                                <CheckBox Name="chkDiscountPercent" Content="Percent?" IsChecked="{Binding bitIsPercentageDiscount, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}" IsEnabled="False"/>
                            </StackPanel>
                        </DataTemplate>
                    </DataGridTemplateColumn.CellTemplate>
                </DataGridTemplateColumn>
            </DataGrid.Columns>
        </DataGrid>

代码:

private Receipt receipt;
private ObservableCollection<ReceiptItem> ocReceiptItem;

private void UpdateCartAndTotals()
{
    TransactionManager TM = new TransactionManager();
    receipt = TM.ReturnReceiptTotalsAndCart(receipt);
    ocReceiptItem = receipt.lstReceiptItem;
    grdSalesCart.DataContext = ocReceiptItem; 
    //DataContext = ocReceiptItem;
    //grdSalesCart.ItemsSource = DGM.ReturnSelectedDataTable(13, criteria).DefaultView;
}

我尝试过的事情:

  • 我没有设置DataContext,而是尝试设置ItemsSource时没有运气
  • 在DataGrid定义中设置DataContext
  • 在DataGrid定义中设置ItemsSource
  • 将ocReceiptItem更改为ReceiptItem类型的列表
  • 将收据和ocReceiptItem更改为公开

我看过的其他一些stackoverflow问题:

0 个答案:

没有答案