更新Xamarin的数据网格

r1zhe5dt  于 2023-03-16  发布在  其他
关注(0)|答案(1)|浏览(138)

我有一个由“Xamarin form DataGrid”组成的数据网格。当我更新/更新数据内容时,我发现它不跟随刷新。我错过了什么使它刷新的事情吗?任何帮助都是值得赞赏的。
特里·比尔

yrwegjxp

yrwegjxp1#

当我续订/更新数据内容时,我发现它没有跟随刷新。
您使用的是nuget Xamarin.Forms.DataGrid吗?
您可以重新检查是否为物料模型实施了INotifyPropertyChanged
例如,如果希望在更改属性Name的值后自动刷新UI,可以执行以下操作:

public class Professional: INotifyPropertyChanged 
    {
        public string Id { get; set; }

        //public string Name { get; set; }
        private string _name;
        public string Name
        {
            get => _name;
            set
            {
                SetProperty(ref _name, value);
            }
        }
        public string Desigination { get; set; }
        public string Domain { get; set; }
        public string Experience { get; set; }

        bool SetProperty<T>(ref T storage, T value, [CallerMemberName] string propertyName = null)
        {
            if (Object.Equals(storage, value))
                return false;

            storage = value;
            OnPropertyChanged(propertyName);
            return true;
        }

        protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }

如果您希望在从ItemsSource列表中添加或删除项时自动刷新UI,则可以将数据列表的类型定义为ObservableCollection<>,例如:

public ObservableCollection<Professional> Professionals {get;set;}

相关问题