WPF Datagrid - adding and deleting rows and MVVM

橙三吉。 提交于 2020-02-04 01:30:14

问题


If my DataGrid is 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 ObservableCollection tied to it?

Do I have to do a command for this to work? Does it not just work with just binding to the collection?

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

回答1:


DataGrids do not automatically do that with observable collections. The deletes are held in memory and you have to hook into the CollectionChanged event to inspect inserts and deletions.

It's all spelled out here: http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples#updates




回答2:


I went ahead and used a BindingList instead of an ObservableCollection in my view model, it seems to work for what I need.



来源:https://stackoverflow.com/questions/12629281/wpf-datagrid-adding-and-deleting-rows-and-mvvm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!