Search code examples
c#wpflistviewobservablecollection

Listview item is not updating when there is changes in Sqlite Db


I'm trying to refresh Listview items if there is some changes in DB. But only working method what i found was adding items to my List and then removing them and adding again using timer.

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

or using this code.

CollectionViewSource.GetDefaultView(Contacts).Refresh();

In list view I'm using Marquee Text Animation. so for this case when i use timer it's refreshing items and ruining my text animation. I think I'm using INotifyPropertyChanged wrong and that should be the reason. Can someone help me figure out this one :D
.xaml code

            <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>

Main view 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));
        }
    }

Contact 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; }
    }

Solution

  • Adding INotifyPropertyChanged to my ContactModel Fixed my issue. Big thx to @emoacht

    Example code from my ContactModel if someone need it.

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