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

时间:2015-03-10 01:13:27

标签: c# wpf datagrid observablecollection

我正致力于在可观察集合中添加和删除行。

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

型号:

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>

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

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不会更新视图。

1 个答案:

答案 0 :(得分:2)

您需要将DataContext的{​​{1}}设置为您的视图模型:

MainWindow

我还建议在视图模型中查看绑定点击事件,而不是依赖于后面的代码。

相关问题