?
Solved

WPF Refresh form when bound data changes

Posted on 2010-11-19
6
Medium Priority
?
1,389 Views
Last Modified: 2012-08-13
I have a standard MVVM pattern where the data is exposed to my view from my viewmodel using property getters and setters. For example,

public String Phone
        {
            get { return _info.Phone; }
            set
            {
                _info.Phone = value;
                RaisePropertyChanged("Phone");
            }
        }

Where _info is the .CurrentItem in the ListCollectionView. When the user clicks on the 'Next' button I advance to the next record with:
    _infoView.MoveCurrentToNext();
    _info = (Info)_infoView.CurrentItem;

The question is what is the best way to inform WPF to refresh the form?
I have tried two different ways which both work but neither seems elegant:

1) repeat RaisePropertyChanged("xxx") for every field on the phone. That is way too verbose for my taste.

2) window3.DataContext = null; window3.DataContext = this;
where 'this' is the VIewModel class itself. (If I don't set it to null first the form does not change.)
0
Comment
Question by:esc_toe_account
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 5

Expert Comment

by:DeviantSeev
ID: 34268651
Have you looked at using an ObservableCollection?

http://msdn.microsoft.com/en-us/library/ms668604.aspx
0
 

Author Comment

by:esc_toe_account
ID: 34343449
Thanks for the suggestion. I will have to remember about Observable Collections when I am doing grids or lists, etc. However, I do not see how an observable collection solves this issue for several reasons:

1) no data is being added or changed so there is no event to notify the UI about - only the current record pointer is being moved.
2) this is an MVVM implementation so there is no collection of ModelVIew records - there is only the single ModelView object.
3) the ModelView also contains Commands and there is only one command to bind to; hence, once again no possible collection of objects.
4) Observable collection seems more fitting for a display consiting of a ListBox of Treeview or some such 'collection' which does not exist in this case.
0
 
LVL 5

Expert Comment

by:DeviantSeev
ID: 34344426
No problem.

I don't know if you have figured out your problem or not yet so I am going to just give you some suggestions. In your MVVM scenario, INotifyPropertyChanged is the way to go. (Even if it's too verbose for your taste)

If you don't want to go with a method which automatically let's the UI know that it's time to update itself then you can call InvalidateVisual() yourself as needed.
0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 

Author Comment

by:esc_toe_account
ID: 34346260
I tried InvalidateVisual() on the window/page, on the grouping container and even on the textbox itself and none of them refreshed the display. I saw someone else suggest you needed UpdateLayout() but that didn't work either.

I also have some code I wrote a while back to refresh the display on another thread:

private static action EmptyDelegate - deletege() {};
public static void Refresh ( this UIElement uiElement)
}
      uiElement.dispatcher.Invoke((DispatcherPriority.Render, EmptyDelegate);
}

However, this code also won't refresh the display?!?

So, the only thing that works is still:
view.DataContext=null; view.DataContext=this;

0
 

Accepted Solution

by:
esc_toe_account earned 0 total points
ID: 34363551
A co-worker here found the answer which is to pass a null argument, rather than the string property name, into the RaisePropertyChanged(null) method. Apparently that tells WPF to refresh all UIElements.

I like it because this must be more efficient that setting .DataContext=null since this latter approach would additionally require WPF to re-establish all the bindings.
0
 

Author Closing Comment

by:esc_toe_account
ID: 35455633
Provided by a co-worker
0

Featured Post

Not sure which OpenStack Certification to get?

So you’ve realized you might want to get certified in OpenStack, but you’re not sure what the benefits might be or even which one you should take. You know there are several certification courses you can choose from, but how do you know which one is right for you?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IP addresses can be stored in a database in any of several ways.  These ways may vary based on the volume of the data.  I was dealing with quite a large amount of data for user authentication purpose, and needed a way to minimize the storage.   …
It seems a simple enough task, yet I see repeated questions asking how to do it: how to pass data between two forms. In this article, I will show you the different mechanisms available for you to do just that. This article is directed towards the .N…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question