数据网格项源为空 [英] Datagrid itemssource is empty

查看:35
本文介绍了数据网格项源为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我从数据网格中选择 itemsource 时,该值为空.是不是因为我没有在datagrid中定义imtemsource?

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});

这是我的 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>

推荐答案

您应该使用绑定向数据网格添加项目:

You should use binding to add items to datagrid:

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

并且在您的视图模型(或分配给您视图的 DataSource 的东西)中:

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

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

ObservableCollection 是 wpf 绑定中使用的一个,因为它实现了允许数据网格对这个集合中的变化做出反应的模式.

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

这篇关于数据网格项源为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆