WPF Datagrid-添加和删除行和MVVM [英] WPF Datagrid - adding and deleting rows and MVVM

查看:241
本文介绍了WPF Datagrid-添加和删除行和MVVM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我的 DataGrid 绑定到MVVM属性,并且用户删除或向网格添加了一行,它是否不应该自动从 ObservableCollection 绑定了吗?

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?

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


推荐答案

我继续使用了 BindingList 而不是 ObservableCollection 在我的视图模型中,它似乎可以正常工作满足我的需求

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

这篇关于WPF Datagrid-添加和删除行和MVVM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆