[英]Binding list into datagrid
Having hard time binding a list to the datagrid: 难以将列表绑定到datagrid:
XAML: XAML:
<DataGrid Name="dgProductList"
ItemsSource="{Binding ProductList}">
<DataGridTextColumn Binding="{Binding ProductName}" Header="Item" />
<DataGridTextColumn Binding="{Binding Quantity}" Header="Qty" />
<DataGridTextColumn Binding="{Binding TotalPrice}" Header="Price" />
<DataGridTemplateColumn Width="*">
Code behind: 代码背后:
public IList<SalesItem> ProductList = new List<SalesItem>();
public void LoadProduct(Product product)
{
SalesItem item = new SalesItem { ProductName = product.Name, Quantity = 1, TotalPrice = product.Price };
ProductList.Add(item);
}
It does not load. 它没有加载。
Thank you. 谢谢。
Try to bind your collection to 尝试将您的收藏绑定到
ObservableCollection<SalesItem>
The UI is probably not notified when you add a product. 添加产品时可能不会通知UI。
The dataGrid won't know when the ProductList is updated... make a ObservableCollection like this: dataGrid不知道ProductList何时更新...像这样创建一个ObservableCollection:
public ObservableCollection<SalesItem> ProductList = new ObservableCollection<SalesItem>();
EDIT: 编辑:
Is the binding working at all? 绑定是否有效? Name your control/window Root and bind to that element
将控件/窗口命名为Root并绑定到该元素
<Window x:Class="test.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525" x:Name="Root">
<Grid>
<DataGrid Name="dgProductList" ItemsSource="{Binding ElementName=Root,Path=ProductList}">
<DataGridTextColumn Binding="{Binding ProductName}" Header="Item" />
<DataGridTextColumn Binding="{Binding Quantity}" Header="Qty" />
<DataGridTextColumn Binding="{Binding TotalPrice}" Header="Price" />
</DataGrid>
</Grid>
</Window>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.