繁体   English   中英

ItemsSource更改时,DataGrid不更新

[英]DataGrid not updating when ItemsSource changes

我有一个AccountManager类,它被实现为Singleton class

namespace SampleApp.Manager
{
    public class AccountManager : INotifyCollectionChanged, INotifyPropertyChanged
    {
        public static AccountManager Instance { get; } = new AccountManager();

        public event NotifyCollectionChangedEventHandler CollectionChanged;
        public event PropertyChangedEventHandler PropertyChanged;

        private List<Account> InternalAccounts { get; } = new List<Account>();

        public IReadOnlyCollection<Account> Accounts => InternalAccounts;

        private AccountManager()
        {

        }

        public void Add(Account account)
        {
            InternalAccounts.Add(account);

            CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add, account));
            OnPropertyChanged(nameof(Accounts));
        }

        public void Remove(Account account)
        {
            if (InternalAccounts.Remove(account))
            {
                CollectionChanged?.Invoke(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Remove, account));
                OnPropertyChanged(nameof(Accounts));
            }
        }

        protected virtual void OnPropertyChanged(string propertyName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

然后,在我要使用这些Account实例的地方有Page

<Page x:Class="SampleApp.View.AccountView"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
      xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
      xmlns:mgr="clr-namespace:SampleApp.Manager"
      mc:Ignorable="d" 
      d:DesignWidth="1200" d:DesignHeight="800"
      Title="AccountView">

    <Grid>

        <TextBlock Text="{Binding Source={x:Static mgr:AccountManager.Instance}, Path=Accounts.Count}" />

        <DataGrid ItemsSource="{Binding Source={x:Static mgr:AccountManager.Instance}, Path=Accounts}">

            <DataGrid.Columns>
                <DataGridTextColumn Header="Property1" Binding="{Binding Property1, Mode=TwoWay}" Width="*" />
                <DataGridTextColumn Header="Property2" Binding="{Binding Property2, Mode=TwoWay}" Width="*" />
                <DataGridTextColumn Header="Property3" Binding="{Binding Property3, Mode=TwoWay}" Width="*" />
                <DataGridTextColumn Header="Property4" Binding="{Binding Property4, Mode=TwoWay}" Width="*" />
            </DataGrid.Columns>

        </DataGrid>

    </Grid>
</Page>

我首先尝试仅触发AccountManager.CollectionChanged来更新视图中的DataGrid ,但这没有任何作用。 因此,接下来我实现了INotifyPropertyChanged并触发了AccountManager.PropertyChanged但是它没有更新列表,而是仅更新了包含AccountManager.Accounts当前项目数的TextBlock

这只是类实际外观的简化版本,这就是为什么我不使用ObservableCollection<T>来执行此操作的原因,所以请不要怪我不使用ObservableCollection<T>

我想知道我的代码有什么问题吗? 我不明白为什么DataGrid绑定没有得到更新,但是TextBlock得到了更新。

InternalAccounts是一个List ,而不是ObservableCollection 您永远不会告诉任何人您的列表已更改。 在拥有它的视图INotifyCollectionChanged上实现INotifyCollectionChanged并不能帮助List引发任何事件:当List发生更改时,您确实引发了一些事件,表明您的ViewModel 自己的项目已更改,但实际上没有任何项目-它的项目位于列表,这是XAML绑定到的列表。

如果将InternalAccountsObservableCollection并将Accounts ReadOnlyObservableCollection ,则应该对其进行修复。

private OnlyObservableCollection<Account> _accounts = 
    new OnlyObservableCollection<Account>();

private ReadOnlyObservableCollection<Account> _roAccounts;
public ReadOnlyObservableCollection<Account> Accounts
{
    get { 
        if (_roAccounts == null)
            _roAccounts = new ReadOnlyObservableCollection<Account>(_accounts);
        return _roAccounts;
    }
}

在内部,只需从_accounts添加和删​​除内容,就不必在视图INotifyCollectionChanged上实现INotifyCollectionChanged 您刚刚获得了免费的完整实现,即可使用。 这比您想象的要容易得多,这总是很好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM