0

I have searched for hours and still do not know why when changes are made to my database why it is not reflecting immediately on the TextBlock item in my ListBox.

Here is my class:

class Event : INotifyPropertyChanged
{
   public string Name { get; set; }
   public event PropertyChangedEventHandler PropertyChanged;

    public string CustomerName
    {
        get
        {
            return this.Name;
        }

        set
        {
            if (value != this.Name)
            {
                this.Name = value;
                NotifyPropertyChanged("CustomerName");
            }
        }
    }

    private void NotifyPropertyChanged(String propertyName = "")
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}

Here is my xaml
<ListBox x:Name="lb_events" ItemTemplate="{DynamicResource EventsTemplate}"...
<DataTemplate x:Key="EventsTemplate"...<StackPanel><TextBlock Text="{Binding CustomerName, UpdateSourceTrigger=PropertyChanged, Mode=TwoWay}"

The TextBlock displays the Event.Name assigned, but when I change the data in the database, changes are not made until I reinitialize the binding.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
kevin yu
  • 17
  • 6
  • 3
    How binding will come to know that property is updated in database? It only listen to the property to which it is binded to. Have you updated CLR property `CustomerName` when it gets updated in database? – Rohit Vats Feb 15 '14 at 12:34
  • It will never work, as you described. binding maps to property projection in memory from one side, and ui from other side. no db involved here. – Eugene P. Feb 15 '14 at 12:36
  • so there is no way without recalling the bind method for it to show the updates made from database table? Would you recommend a method to keep refreshing the window to show the update? Maybe a checkbox "Live Update" where if checked, it will keep refreshing the bind until it is unchecked? I'd imagine this is not very efficent. Thank you. – kevin yu Feb 15 '14 at 12:40

2 Answers2

0

A WPF binding binds the UI to the ViewModel. Changes in the model (database or other data source) will not automatically notify the ViewModel. And If the ViewModel does not know of the changes, it cannot update the UI through the binding.

If you need to react on changes in your model, you will need to find a way to notify your viewmodel of those changes.

nvoigt
  • 75,013
  • 26
  • 93
  • 142
0

As suggested by others, you would need to notify viewmodel if your model (database) updates.

Would you recommend a method to keep refreshing the window to show the update?

A simple solution could be to have Updated event in your model (which gets/sets database), and then your viewmodel will attach a handler to this event. On recieving any update, ViewModel would re-initialize itself which will get reflected in UI.

If your design doesn't support updating model when database is updated. Then it is bigger design quesion really. For instance, how database is updated? are you using any ORM?...

Manish Basantani
  • 16,931
  • 22
  • 71
  • 103
  • I am sorry, I am new to WPF and .NET and do not understand this viewmodel. I currently have a Windows Service application which updates my database and is different application than my WPF project. Can you recommend any source where I can study up on what viewmodel is? Thank you. – kevin yu Feb 15 '14 at 12:49
  • No need to be sorry.. we all start with some new technology at some point. How does your WPF application retrives data? is it through another service, through ORM, or direct database query? – Manish Basantani Feb 15 '14 at 12:52
  • it is through direct database query. – kevin yu Feb 15 '14 at 12:52
  • Okay. Then you have option of database polling, but won't suggest that. You may go with the checkbox option, depending on scale of your application. this post seems related: http://stackoverflow.com/questions/15915164/how-can-i-alert-my-program-if-a-specific-database-table-has-been-added-updated-d – Manish Basantani Feb 15 '14 at 13:00