Solved

WPF Refresh form when bound data changes

Posted on 2010-11-19
6
1,364 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

707 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now