可以从 Observable Collection 中删除但视图不更新

Can delete from Observable Collection but view does not update

我正在研究在可观察集合中添加和删除行的能力。

从最初的 post 开始,我创建了一个测试应用程序,它只能从可观察集合中删除行。我在外部填充数据库然后在此打开它只是为了测试不起作用的删除功能。它执行 RemoveAt 行,从 Observable 集合中删除,但视图不更新。这是我的所有代码:

型号:

public class TestModel : ObservableObject
{
    #region Properties
    private Double id;
    public Double ID
    {
        get { return id; }
        set
        {
            id = value;
            RaisePropertyChangedEvent("ID");
        }
    }

    private string type;
    public string Type
    {
        get { return type; }
        set
        {
            type = value;
            RaisePropertyChangedEvent("Type");
        }
    }

    private decimal amount;
    public decimal Amount
    {
        get { return amount; }
        set
        {
            amount = value;
            RaisePropertyChangedEvent("Amount");
        }
    }

    private string notes;
    public string Notes
    {
        get { return notes; }
        set
        {
            notes = value;
            RaisePropertyChangedEvent("Notes");
        }
    }
    #endregion
}

视图模型:

public class MainWindowViewModel : ObservableObject
{
    #region GetData
    public MainWindowViewModel()
    {
        Transactions = DatabaseFunctions.getTransactionData();
    }
    #endregion

    #region ObservableCollections

    private ObservableCollection<TestModel> transactions;
    public ObservableCollection<TestModel> Transactions
    {
        get { return transactions; }
        set
        {
            transactions = value;
            RaisePropertyChangedEvent("Transactions");
        }
    }
    #endregion

    #region Properties
    public static string SharedWith;

    private Double id;
    public Double ID
    {
        get { return id; }
        set
        {
            id = value;
            RaisePropertyChangedEvent("ID");
        }
    }

    private string type;
    public string Type
    {
        get { return type; }
        set
        {
            type = value;
            RaisePropertyChangedEvent("Type");
        }
    }

    private decimal amount;
    public decimal Amount
    {
        get { return amount; }
        set
        {
            amount = value;
            RaisePropertyChangedEvent("Amount");
        }
    }

    private string notes;
    public string Notes
    {
        get { return notes; }
        set
        {
            notes = value;
            RaisePropertyChangedEvent("Notes");
        }
    }
    #endregion

    public void DeleteTransactionRow(List<TestModel> SelectedTransaction, int SelectedIndex)
    {
        Transactions.RemoveAt(SelectedIndex);
    }

查看:

<Window x:Class="OCTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:Properties="clr-namespace:OCTest.Properties"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"
    Title="Test" SizeToContent="WidthAndHeight"
    xmlns:ViewModel="clr-namespace:OCTest.ViewModel">

<Window.DataContext>
    <ViewModel:MainWindowViewModel/>
</Window.DataContext>
<Grid>
    <xcdg:DataGridControl x:Name="TransactionsDataGrid"  Grid.Row="0" ItemsSource="{Binding Transactions, Mode=TwoWay}" AutoCreateColumns="False" SelectionMode="Single">
        <xcdg:DataGridControl.ContextMenu>
            <ContextMenu>
                <MenuItem Header="Delete Row" Click="DeleteTransactionRow_Click"/>
            </ContextMenu>
        </xcdg:DataGridControl.ContextMenu>

        <xcdg:DataGridControl.Columns>
            <xcdg:Column Title="Type"  FieldName="Type" ReadOnly="True"/>
            <xcdg:Column Title="Amount" FieldName="Amount">
                <xcdg:Column.CellContentTemplate>
                    <DataTemplate>
                        <TextBlock Text="{Binding StringFormat={}{0:C}}"/>
                    </DataTemplate>
                </xcdg:Column.CellContentTemplate>
            </xcdg:Column>
            <xcdg:Column Title="Notes" FieldName="Notes"/>
        </xcdg:DataGridControl.Columns>
    </xcdg:DataGridControl>
</Grid>

处理删除命令并将所需信息传递给视图模型的代码:

public partial class MainWindow : Window
{
    MainWindowViewModel mainwindowviewmodel = new MainWindowViewModel();

    public MainWindow()
    {
        InitializeComponent();
    }

    private void DeleteTransactionRow_Click(object sender, RoutedEventArgs e)
    {
        List<TestModel> selectedtransaction = TransactionsDataGrid.SelectedItems.Cast<TestModel>().ToList();
        mainwindowviewmodel.DeleteTransactionRow(selectedtransaction, TransactionsDataGrid.SelectedIndex);
    }

    private void MouseRightButtonUpHandler(object sender, RoutedEventArgs e)
    {
        this.TransactionsDataGrid.SelectedItem = ((DataCell)sender).ParentRow.DataContext;
    }
}

希望有人能明白 RemoveAt 不更新视图的原因。

您需要将 MainWindowDataContext 设置为您的视图模型:

 public MainWindow()
 {
    InitializeComponent();

    DataContext = new MainWindowViewModel();
 }

我还建议研究将点击事件绑定到视图模型中的命令,而不是依赖背后的代码。