WPF的Datagrid绑定不更新,直到单击行头 [英] WPF Datagrid Binding Doesn't Update Until Clicking Row Header

查看:739
本文介绍了WPF的Datagrid绑定不更新,直到单击行头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个绑定到视图模型的列表的数据网格。网格的内容不更新,直到我点击行标题。点击各种细胞并不影响它。我必须点击标题。

I have a datagrid that is bound to a List in the view model. The grid's contents don't update until I click on the row header. Clicking in various cells doesn't affect it. I have to click on the header.

这是在XAML数据网格:

This is the datagrid in the XAML:

<DataGrid x:Name="TransactionDetailsGrid" Grid.Row="1" AutoGenerateColumns="False" SelectionMode="Extended" IsReadOnly="True" HeadersVisibility="Column"
                  ItemsSource="{Binding TransactionDetailList}" SelectedItem="{Binding SelectedTransactionDetail}" GridLinesVisibility="None">
    <DataGrid.Columns>
        <DataGridTextColumn Binding="{Binding Path=Account.AccountNumber}" Header="Account No." HeaderStyle="{StaticResource DataGridHeaderStyleCenter}" Width="120" />
        <DataGridTextColumn Binding="{Binding Path=Account.AccountName}" Header="Account Name" HeaderStyle="{StaticResource DataGridHeaderStyleCenter}" Width="*" />
        <DataGridTextColumn Binding="{Binding Path=Amount}"  Header="Amount" HeaderStyle="{StaticResource DataGridHeaderStyleCenter}" Width="120" />
    </DataGrid.Columns>
</DataGrid>

这是从视图模型:

And this is from the view model:

public List<TransactionDetail> TransactionDetailList
{
    get { return this._transactionDetailList; }

    set
    {
        this._transactionDetailList = value;
        RaisePropertyChanged("TransactionDetailList");                
    }
}

这是的项目之一的编辑,在视图模型

This is the edit of one of the items, in the view model:

private void AddTransactionDetail()
{
    TransactionDetailViewModel viewModel = new TransactionDetailViewModel();

    MainWindowViewModel.ViewLoader.ShowDialog(viewModel);

    if (viewModel.TransactionDetail != null)
    {
        this.TransactionDetailList.Add(viewModel.TransactionDetail);
        RaisePropertyChanged("TransactionDetailList");
    }
}

这运行后,我可以把TransactionDetailList的getter断点,集合在它的项目。然而,数据网格是空的。如果我点击标题行,该项目显示在网格中。

After this runs, I can put a breakpoint on the getter of TransactionDetailList, and the collection has the item in it. However, the datagrid is empty. If I click on the header row, the item shows up in the grid.

我有同样的问题在做编辑的时候。

I have the same issue when doing an edit.

我已经成功地做​​过,所以我不知道有什么不同,这里。我失去了一些东西明显?为什么不网格显示其内容,直到我点击标题行?

I've done this successfully before, so I'm not sure what's different here. Am I missing something obvious? Why won't the grid show its contents until I click on the header row?

我只注意到一些有趣的事情。当我点击的网格标题,在TransactionDetailList吸气断点没有被打到,但数据仍显示。所以,它就像网格具有的信息,它只是没有显示它的头被点击,直到

I just noticed something interesting. When I click on the grid header, the breakpoint in the TransactionDetailList getter doesn't get hit, but the data still shows up. So, it's like the grid has the info, it's just not showing it until the header is clicked.

改变使用的ObservableCollection后,它的工作。但现在我在与编辑同样的问题(网格不更新,直到点击标题):

After changing to use ObservableCollection, it worked. But now I'm having the same problem with the edit (grid doesn't update until clicking the header):

private void EditTransactionDetail()
{
    TransactionDetailViewModel viewModel = new TransactionDetailViewModel(this.SelectedTransactionDetail);

    MainWindowViewModel.ViewLoader.ShowDialog(new TransactionDetailViewModel(this.SelectedTransactionDetail));

    RaisePropertyChanged("TransactionDetailList");
}

请问我的实体需要实现INotifyPropertyChanged?如果我改变了收集,并调用RaisePropertyChanged,应该不会导致更新到电网?

Does my entity need to implement INotifyPropertyChanged? If I change the collection, and call RaisePropertyChanged, shouldn't that cause an update to the grid?

推荐答案

现在的问题是,当你从集合中添加或删除项目,二传手不叫。这意味着 INotifyPropertyChanged的不叫,并且认为已经没有办法知道它需要被刷新。

The problem is that when you add or remove an item from the collection, the setter isn't called. This means INotifyPropertyChanged isn't called, and the view has no way of knowing it needs to be refreshed.

通过WPF也支持 INotifyCollectionChanged 接口解决这个问题。

WPF solves this by also supporting the INotifyCollectionChanged interface.

尝试使用的ObservableCollection&LT; TransactionDetail&GT; ,而不是列表&LT; TransactionDetail&GT; 的ObservableCollection&LT; T&GT; 是内置的,并实现了 INotifyCollectionChanged ,这样你就不必做太多的$ C $角

Try using an ObservableCollection<TransactionDetail> instead of a List<TransactionDetail>. ObservableCollection<T> is built in, and implements INotifyCollectionChanged, so you won't have to do much to your code.

另外,还要确保你继续实施 INotifyPropertyChanged的在您的视图模型,你已经离开。这样,当您更换整个集合(当调用setter时)视图将被通报。

Also make sure that you continue to implement INotifyPropertyChanged on your view model as you already have. This way the view will be notified when you replace the entire collection (when the setter is called).

public ObservableCollection<TransactionDetail> TransactionDetailList
{
    get { return this._transactionDetailList; }

    set
    {
        this._transactionDetailList = value;
        RaisePropertyChanged("TransactionDetailList");                
    }
}

请参阅:

  • http://msdn.microsoft.com/en-us/library/ms668604.aspx
  • http://msdn.microsoft.com/en-us/library/ms748365.aspx (although this isn't using MVVM style)

修改

此外,你应该实施 INotifyPropertyChanged的 TransactionDetail 太。如果不能,它包装在一个类中确实实现 INotifyPropertyChanged的

Also, you should implement INotifyPropertyChanged on TransactionDetail too. If you can't, wrap it in a class that does implement INotifyPropertyChanged.

如果你不执行它 TransactionDetail ,不是改变你做不影响之列,但在做冲击性能 TransactionDetail 实例,不会显示在UI中,直到你不知何故刷新整个列表。

If you don't implement it on TransactionDetail, than changes you make that don't impact the list, but do impact properties on a TransactionDetail instance, won't show in the UI until you somehow refresh the whole list.

如果你试图通过调用 RaisePropertyChanged 在列表属性来解决这个问题,那么你的UI认为整个列表(因此整套UI对象)必须为抛出和更新。这会杀了你的表现,让您的应用程序缓慢。

If you tried to fix this by calling RaisePropertyChanged on the list property, then your UI thinks the entire list (and hence the whole set of UI objects) needs to be thrown out and updated. This will kill your performance and make your app sluggish.

这篇关于WPF的Datagrid绑定不更新,直到单击行头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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