WPF data-binding manual update - wpf

I have a List<Foo> from a non-WPF assembly which I'm attempting to databind to a WPF <ListBox>. Initially, the list items display correctly, but when I add a new item to the List<Foo>, the listbox doesn't add a list item. How do I tell the list box to re-bind / update / refresh the data and show the new item?

Although using an ObservableCollection is the best way, to answer the actual question, the way to update manually is to call BindingExpression.UpdateTarget

You should use a ObservableCollection instead, then you'll get updates automatically.

Thanks for posting this answer. Even if you use ObservableCollection, you may need to use BindingExpression.UpdateTarget. This can be the case if the collection is not in the UI thread. I've been writing some multi-threaded WPF apps, and I've been finding myself having to strip out data binding when I move model code to another thread, because I can't count on the update system to really work. While I find data binding to be a great concept, I think the opaqueness of the data binding system has been a real hindrance for my adoption of it. (Sorry for the rant!) Thanks again, Adam.

Related

Listening a event from other form in WPF

Do you know how event from "Add_Form" can be listened from "MainForm"? I code in WPF 2010(Visual Studio).
My program has two forms - "MainForm" and "Add_Form". "MainForm" has "Student_DataGrid" which has a register of students and button("btn_add_student") which invokes "Add_Form"(Form which has textblocks for input data of students). After clicking "btn_add_student" shows "Add_Form", further, I fill in data in textblocks and push the button "btn_add_student". New student is added in Data Base and "Add_form" is closed. Then "Student_DataGrid" on "MainForm" must be reloaded.
In other words I want to listen event on Add_Form from MainForm. I can do it in C#, but I can't do it in WPF. I'll be glad to any help.
Seems you haven't explored the strength of WPF i.e. Binding. These tutorials will get you going Binding Tutorial and one from msdn.
All you need to do in your case is bind your grid to an ObservableCollection and while adding a new student, add the object in this collection. Grid will automatically get refreshed.
If "Add_Form" and "MainForm" are binding to the same source, "Student_DataGrid" on "MainForm" needn't reloaded.
WPF can do it for u.
I found the way to solve it:).
I just made whole class of events:).
Here you are!

WPF ObservableCollection Edit Mode

I am using observable collections all around my applications. My problem is that when i use a popup window for editing those entities, my bound lists are getting changed when the user changes those corresponding fields in the window.
How could i simply freeze the observable changes norifications, and release them only when the entity is saved?
Thanks,
Oran
I think the issue is not with the collection, but with the entities themselves. ObservableCollection raises an event when an item is added or removed, not when a property of an item is changed. This part is handled by the INotifyPropertyChanged implemented by the item, so it's this notification you need to disable.
I suggest you have a look at the IEditableObject interface, which is designed for this kind of scenario. You can disable the notifications in the BeginEdit method, and reenable them in EndEdit and CancelEdit.
EDIT: Paul Stovell has a nice implementation of an IEditableObject wrapper here :
http://www.paulstovell.com/editable-object-adapter
You can use:
BoundPropertyOfViewModel = CollectionViewSource.GetDefaultView(AgentDeploymentDetail);
and bind to the view instead of binding directly to the ObservableCollection. This is the same object that allow you to filter/sort your output without touching the collection.
When you want to stop changes, use DeferRefresh(). When you are done, call Refresh().
WARNING
This will not pervent showing of changes in each item itself, only the list.
You could make a deep copy of the object you want to edit. This way, you can act on the copy while editing, without interfering with the original that remains in the list. Once you`re done editing, you can replace the original by the edited version or rollback.
All the anwers above are great. but i found a good and convinent prodedure to perform the desired in an efficient and clean way. It is based on performing a deep copy on a detached object, using Matthieu MEZIL entity cloner ( http://msmvps.com/blogs/matthieu/archive/2008/05/31/entity-cloner.aspx ).
For full details please check out the followings : Entity Framework Attach Exception After Clone
Thanks for all the great support...

C# 4.0 .net 4 and WPF UI update

I'm trying to build an app to work with twitter like site , and the problem I'm trying to solve is -
How am i going to update the UI with a background worker and only add show the latest posts(tweets if you will )
on top of the wrap panel without removing the ones that already exist?
in my previous attempt i have done this by storing the tweets/posts in a local SQlite database and then retrieving the last 10 posts which causes the UI to freeze for a bit and reload all the posts.
Any ideas ?
Since you're using WPF, this is fairly easy.
Just store your "posts" in an ObservableCollection<T>. You can then just Insert the new item at the front of the list. If you've bound this to an ItemsCollection control, WPF will handle redrawing everything correctly for you.
Unless you have a LOT of elements, this will work quite well, and be fairly fast. I'd try it first (since it's really easy to implement), and only try to get more "clever" if you find that it has performance problems. I doubt you'll have issues, though, provided you use the proper collections, since WPF's data binding to ObservableCollection is quite fast.
Use databinding. The UI elements are databound to fields of a record in a collection. The panel of multiple rows should be an ItemCollection of some sort - Listbox, most likely - so that it will replicate the item data template for each row in the underlying data collection.
After that, the UI will track with any changes made to the underlying collection, if the collection implements INotifyCollectionChanged. If you use an in-memory collection, you can just add new data to the top of the collection (Insert at index 0) and that will push all the old items down in the UI display. If you use a file or server based data source, you can fetch the data in a background thread and post updates to the in-memory collection on the foreground thread. Just don't update the databound collection from the background thread.

WPF Keep column sorting with Datagrid

I have several datagrid where I need to update the informations. Things is, since more than one person works on the system at the same time, the datagrid need to be refreshed on a regular basis. When I refresh, I lose the sorting that user had.
Is there a way to keep it?
thanks
Just update the contents of the bound collection - don't replace the collection itself. Then you will not get a new CollectionView so your sorting won't be affected.
Note that this is untested, but could you do something like this?
ListCollectionView lcv = (ListCollectionView)CollectionViewSource.GetDefaultView(myDataGrid.ItemsSource);
IComparer mySort = lcv.CustomSort; // assumes you've already set it beforehand
... // stuff happens
lcv.CustomSort = mySort;
I am still learning WPF myself, but hope this is some help...
-Matt.

How do I Determine the Source of the SelectionChangedEvent

I have a question regarding a ComboBox in silverlight and it's selected item.
I would like to determine what triggered the SelectionChangedEvent, was it the user selecting a new item in the list or was it programatically set?
While ideally I would like to solve this using the CommandPattern (I am essentially using a modified RelayCommand (http://joshsmithonwpf.wordpress.com/2008/11/17/emulating-icommandsource-in-silverlight-2/). I am open to other suggestions.
I have also played around with the SelectionChangedEventArgs, which has an OriginalSource property, which upon first inspection may appear to help, however it is null (regardless of the manner in which the item was selected.)
Any ideas, other than setting an internal flag? :)
Thanks
Unfortunately this is a tough thing to determine, since the framework works pretty hard to simply bubble up any changes or user events in this situation as that selection changed event.
If you really need to, you could write a simple ComboBoxWrapper that is effectively the flag you're talking about - so you could derive from ComboBox, try overriding or hiding the CLR setter for SelectedItem, and then maintain state that way.
Any particular scenario in use here? There may be another way to approach a solution.

Resources