WPF Datagrid - 添加和删除行和 MVVM
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12629281/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
WPF Datagrid - adding and deleting rows and MVVM
提问by DJ Burb
If my DataGridis bound to an MVVM property and the user deletes or adds a row to the grid, shouldn't it automatically add or delete the data from the ObservableCollectiontied to it?
如果我DataGrid绑定到一个 MVVM 属性并且用户删除或添加一行到网格,它不应该自动添加或删除ObservableCollection绑定到它的数据吗?
Do I have to do a command for this to work? Does it not just work with just binding to the collection?
我是否必须执行命令才能使其正常工作?它不仅仅适用于绑定到集合吗?
XAML
XAML
<ExtendedGridControl:ExtendedDataGrid Grid.Row="5" Height="200" VerticalAlignment="Top" Grid.ColumnSpan="6" Margin="5,4,5,0" ItemsSource="{Binding InvoiceDetailsForSelectedJobInvoice, Mode=TwoWay}" AutoGenerateColumns="False">
<DataGrid.Columns>
<ExtendedColumn:ExtendedDataGridTextColumn Header="Description" Width="200*" AllowAutoFilter="False"
Binding="{Binding Detail_Item_Description}" />
<ExtendedColumn:ExtendedDataGridTextColumn Header="Unit" Width="50" AllowAutoFilter="False"
Binding="{Binding Detail_Item_Unit}" />
<ExtendedColumn:ExtendedDataGridTextColumn Header="Unit Price" Width="70"
Binding="{Binding Detail_Item_Unit_Price}" AllowAutoFilter="False"/>
<ExtendedColumn:ExtendedDataGridTextColumn Header="# of Units" Width="70"
Binding="{Binding Detail_Item_Number_Of_Units}" AllowAutoFilter="False"/>
<ExtendedColumn:ExtendedDataGridTextColumn Header="Discount %"
Binding="{Binding Detail_Item_Discount_Percentage}" Width="70" AllowAutoFilter="False"/>
<ExtendedColumn:ExtendedDataGridTextColumn Header="Discount"
Binding="{Binding Detail_Item_Discount}" Width="70" AllowAutoFilter="False"/>
<ExtendedColumn:ExtendedDataGridTextColumn Header="Total" Width="70"
Binding="{Binding Detail_Item_Total_Price}" AllowAutoFilter="False"/>
<DataGridComboBoxColumn Header="Revenue Allocation" Width="100*"
SelectedValueBinding="{Binding Service_That_Revenue_Is_Allocated_To}"
DisplayMemberPath="ServiceName" SelectedValuePath="ServiceID"
ItemsSource="{Binding Source={StaticResource source}}"/>
</DataGrid.Columns>
</ExtendedGridControl:ExtendedDataGrid>
View Model
查看模型
public class InvoiceViewModel: INotifyPropertyChanged
{
public ObservableCollection<InvoiceDetail> InvoiceDetailsForSelectedJobInvoice
{
get
{
if (_selectedInvoice != null)
{
_invoiceDetails = new ObservableCollection<InvoiceDetail>(_selectedInvoice.InvoiceDetails);
return _invoiceDetails;
}
return null;
}
set
{
_invoiceDetails = value;
NotifyPropertyChanged("InvoiceDetailsForSelectedJobInvoice");
}
}
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(String propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
采纳答案by DJ Burb
I went ahead and used a BindingListinstead of an ObservableCollectionin my view model, it seems to work for what I need.
我继续在我的视图模型中使用 aBindingList而不是 an ObservableCollection,它似乎适合我的需要。
回答by Steve Py
DataGrids do not automatically do that with observable collections. The deletes are held in memory and you have to hook into the CollectionChangedevent to inspect inserts and deletions.
DataGrids 不会自动对可观察集合执行此操作。删除保存在内存中,您必须挂钩CollectionChanged事件以检查插入和删除。
It's all spelled out here: http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples#updates
在这里详细说明:http: //www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples#updates

