簡體   English   中英

當 Sqlite Db 發生更改時,Listview 項目未更新

[英]Listview item is not updating when there is changes in Sqlite Db

如果數據庫中有一些更改,我正在嘗試刷新 Listview 項目。 但我發現的唯一工作方法是將項目添加到我的列表中,然后刪除它們並使用計時器再次添加。

Contacts.Clear();
DbModule.LoadLine().ForEach(es => Contacts.Add(es));//Add new items to list from DB.

或使用此代碼。

CollectionViewSource.GetDefaultView(Contacts).Refresh();

在我正在使用的列表視圖中選框文字動畫 . 所以對於這種情況,當我使用計時器時,它會刷新項目並破壞我的文本 animation。 我認為我使用 INotifyPropertyChanged 錯誤,這應該是原因。 有人可以幫我弄清楚這個嗎:D
.xaml 代碼

            <ListView ItemsSource="{Binding Contacts}"
                          SelectedItem="{Binding SelectedContact}"                                                
                    ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                      Background="#FF2F3136"
                      BorderThickness="0"
                      Grid.Row="0"
                ItemContainerStyle="{StaticResource ContactCard}">
                <ListView.Resources>
                    <Style TargetType="Label">
                        <EventSetter Event="MouseRightButtonUp" Handler="OnMouseRightButtonUp"/>
                        <Style.Triggers>
                            <Trigger Property="IsMouseOver" Value="True">
                            </Trigger>
                        </Style.Triggers>
                    </Style>
                </ListView.Resources>
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <!--<StackPanel Orientation="Horizontal" />-->
                        <WrapPanel />
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
            </ListView>

model主視圖:

    class MainViewModel : ObservableObject
    {
        public ObservableCollection<ContactModel> Contacts { get; set; }
        public MainViewModel()
        {
            Contacts = new ObservableCollection<ContactModel>();
            DbModule.LoadLine().ForEach(es => Contacts.Add(es));//Add new items to list from DB.
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(60);
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            foreach (var x in DbModule.LoadLine())
            {
                Contacts.First(d => d.Line == x.Line).Code = x.Code;
                Contacts.First(d => d.Line == x.Line).Batch = x.Batch;
            }
           OnPropertyChanged();
           CollectionViewSource.GetDefaultView(Contacts).Refresh();
        }
}
    class ObservableObject : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        public void OnPropertyChanged([CallerMemberName] string propertyname = null)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
        }
    }

聯系 Model

    public class ContactModel
    {
        public int Line { get; set; }
        public string Code { get; set; }
        public string Batch { get; set; }
        public string Message1 { get; set; }
        public string Message2 { get; set; }
        public int Stacks { get; set; }
        public int StacksActual { get; set; }
        public int Units { get; set; }
        public int UnitsActual { get; set; }
    }

您需要提供已更改的屬性名稱的值

OnPropertyChanged(nameof(Contacts));

完整代碼:

    class MainViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<ContactModel> Contacts { get; set; }
        public MainViewModel()
        {
            Contacts = new ObservableCollection<ContactModel>();
            DbModule.LoadLine().ForEach(es => Contacts.Add(es));//Add new items to list from DB.
            DispatcherTimer timer = new DispatcherTimer();
            timer.Interval = TimeSpan.FromSeconds(60);
            timer.Tick += timer_Tick;
            timer.Start();
        }

        void timer_Tick(object sender, EventArgs e)
        {
            foreach (var x in DbModule.LoadLine())
            {
                Contacts.First(d => d.Line == x.Line).Code = x.Code;
                Contacts.First(d => d.Line == x.Line).Batch = x.Batch;
            }
            OnPropertyChanged(nameof(Contacts));
            CollectionViewSource.GetDefaultView(Contacts).Refresh();
        }

       public event PropertyChangedEventHandler PropertyChanged;
       public void OnPropertyChanged([CallerMemberName] string propertyname = null)
       {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyname));
       }
    }

INotifyPropertyChanged 添加到我的 ContactModel修復了我的問題。 非常感謝@emoacht

如果有人需要,來自我的 ContactModel 的示例代碼。

     class ContactModel : ObservableObject
    {
        private string _Code;
        public string Code
        {
            get { return _Code; }
            set
            {
                _Code = value;
                OnPropertyChanged();
            }
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM