当我从数据网格中选择 itemsource 时,该值为 null。是不是因为我没有在datagrid中定义imtemsource?
这就是我将一行添加到数据网格中的方式:
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>