[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

WPF Refresh form when bound data changes

Posted on 2010-11-19
6
Medium Priority
?
1,392 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
Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

649 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