WPF Datagrid - 添加和删除行和MVVM

时间:2012-09-27 20:10:26

标签: wpf data-binding mvvm datagrid

如果我的DataGrid绑定到MVVM属性并且用户删除或向网格添加了一行,那么它不应该自动添加或删除与其绑定的ObservableCollection数据吗? / p>

我必须执行此命令才能工作吗?它不仅仅适用于绑定到集合吗?

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>

查看模型

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

2 个答案:

答案 0 :(得分:1)

我继续在我的视图模型中使用BindingList而不是ObservableCollection,它似乎适合我需要的东西。

答案 1 :(得分:0)

DataGrid不会自动对可观察集合执行此操作。删除保存在内存中,您必须挂钩CollectionChanged事件以检查插入和删除。

这里全部拼写出来:http://www.codeproject.com/Articles/30905/WPF-DataGrid-Practical-Examples#updates