Datagrid itemssource is empty

2019-08-25 02:04发布

When I select itemsource from datagrid, the value is null. Is it because I did not define imtemsource in datagrid?

This is how I add in a row into the datagrid:

this.dgProductList.Items.Add(new {ProductId=product.ProductId, Name=product.Name, Qty=1, Price=product.Price});

Here is my wpf:

<DataGrid Name="dgProductList" 
                                  AutoGenerateColumns="False" 
                                  FontSize="15"
                                  HorizontalGridLinesBrush="#f0f0f0"
                                  VerticalGridLinesBrush="#f0f0f0"
                                  ScrollViewer.CanContentScroll="False"      
                                  ScrollViewer.VerticalScrollBarVisibility="Auto" 
                                  ScrollViewer.HorizontalScrollBarVisibility="Auto">
                            <DataGrid.Columns>
                                <DataGridTemplateColumn Header="Image">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <Image Height="10" Width="10" Source="{Binding Picture}" Stretch="Uniform"></Image>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                                <DataGridTextColumn Binding="{Binding ProductId}" Visibility="Hidden"/>
                                <DataGridTextColumn Binding="{Binding Name}" Header="Item" />
                                <DataGridTextColumn Binding="{Binding Qty}" Header="Qty" />
                                <DataGridTextColumn Binding="{Binding Price}" Header="Price" />
                                <DataGridTemplateColumn Width="*">
                                    <DataGridTemplateColumn.CellTemplate>
                                        <DataTemplate>
                                            <StackPanel Orientation="Horizontal">
                                                <Button Width="30" Name="btnDecrease" Content="-" />
                                                <Button Width="30" Name="btnIncrease" Content="+" />
                                            </StackPanel>
                                        </DataTemplate>
                                    </DataGridTemplateColumn.CellTemplate>
                                </DataGridTemplateColumn>
                            </DataGrid.Columns>
                        </DataGrid>

标签: c# wpf xaml
2条回答
爱情/是我丢掉的垃圾
2楼-- · 2019-08-25 02:06

you must use ItemsSource property of your grid

like

dgProductList.ItemsSource = YourData;

or

dgProductList.SetBinding(ItemsControl.ItemsSourceProperty, new Binding { 

Source = YourData 
});
查看更多
欢心
3楼-- · 2019-08-25 02:24

You should use binding to add items to datagrid:

<DataGrid ItemsSource="{Binding DataGridItemsSource}" ...

And in your view model (or something that is assigned to DataSource of your view):

public ObservableCollection<ProductData> DataGridItemsSource { get; set; }

ObservableCollection is the one to use in wpf binding because it implements pattern that allows datagrid to react to changes in this collection.

查看更多
登录 后发表回答