【问题标题】:Can delete from Observable Collection but view does not update可以从 Observable Collection 中删除,但视图不会更新
【发布时间】:2015-05-11 08:28:18
【问题描述】:

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

从最初的帖子开始,我创建了一个测试应用程序,它只能从可观察集合中删除行。我在外部填充数据库然后在其中打开它只是为了测试不起作用的删除功能。它执行 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>

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

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

【问题讨论】:

  • 可以发Add方法代码吗?
  • 我已经添加了将行添加到可观察集合的方法
  • 你能把 View Model 和 XAML 的相关部分也贴出来吗?很难对您迄今为止发布的代码提出任何建议。
  • 好的,谢谢您到目前为止的帮助。我相信我已经添加了所有相关代码。
  • 您的数据网格被称为“TestDataGrid”的任何原因,但您使用的是“DataGrid.SelectedIndex”,然后是 itemcollection.RemoveAt(grid.SelectedIndex); - 它叫什么名字?

标签: c# wpf datagrid observablecollection


【解决方案1】:

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

 public MainWindow()
 {
    InitializeComponent();

    DataContext = new MainWindowViewModel();
 }

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

【讨论】:

  • 成功了!我不能感谢你。我正在将所有点击事件绑定到视图模型;一些事情引发了奇怪的错误,但通过这项工作,我可以继续处理这些错误。我只是不明白为什么在代码中设置数据上下文与 XAML 之间存在如此大的差异。但不管怎样,谢谢!
猜你喜欢
  • 2018-11-09
  • 1970-01-01
  • 2022-01-27
  • 2021-12-25
  • 1970-01-01
  • 2013-09-05
  • 1970-01-01
  • 2019-07-04
相关资源
最近更新 更多