Solved

WPF Refresh form when bound data changes

Posted on 2010-11-19
6
1,373 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

810 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