Solved

WPF Refresh form when bound data changes

Posted on 2010-11-19
6
1,379 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
Guide to Performance: Optimization & Monitoring

Nowadays, monitoring is a mixture of tools, systems, and codes—making it a very complex process. And with this complexity, comes variables for failure. Get DZone’s new Guide to Performance to learn how to proactively find these variables and solve them before a disruption occurs.

 

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The object model of .Net can be overwhelming at times – so overwhelming that quite trivial tasks often take hours of research. In this case, the task at hand was to populate the datagrid from SQL Server database in Visual Studio 2008 Windows applica…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

696 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