I am trying to create a program (simple grid - with specialized output) that displays data from an IBindingList data source. I can display the data just fine, but I also want to be able to let the user edit the data in, say in a TextBox that is separate from the grid. My problem is that I am unclear as to how to accomplish the "automatic" updating of the data between the two elements (my grid and the editable textbox).
The list's ListChanged event, I believe, will tell me when the list itself has changed, but I need the grid to know when the actual data in the list has changed. The only thing I have stumbled upon to tell me when a change external to the grid has occurred is to hook onto the BindingManagerBase.Bindings[0].Parse event. Not that a user would do this, but if the user has established several textboxes that are all bound to the same field, I think I would have to loop through each BindingManagerBase.Binding entry and hook it?
I am presuming I am not doing this correctly, and there is a more generalized way to accomplish this. I would GREATLY appreciate some direction from anyone who is more familiar with what I am attempting to accomplish.
Thanks in advance,
-Matt
Have a look at at INotifyPropertyChanged. Implement it on the class that is stored in your BindingList to notify said BindingList when properties on an item change.
If you've implemented IBindingList yourself you'll need to do a little more work to hook up to the events for each item in your list, but the provided BindingList will pick up on these events automatically.
You could also use the generic BindingList and notify the edit using ResetItem
Related
I am having a form with different type of controls like Text Box, Drop downs, Check box, Radio buttons etc. All these controls are loaded dynamically from database at run time.
I want to perform validation on Text box on conditional basis. For example, If we have selected any value in drop down, then you must have to fill details in Text box. Otherwise text box details are not required.
I am open to use database to perform this task and I am using MVVM pattern in my project.
Any help on this is highly appreciated.
Thanks.
(I started this as a comment, but it ended up being too long).
In theory you have access to all these controls and their values in your ViewModel.
Without knowing the specifics of your program, it's difficult to suggest anything useful, but in essence you need to expose some more properties from your ViewModel (probably boolean) which will be calculated based on the values in your controls. Then you need to bind IsEnabled properties on your controls to these new properties.
Sounds simple, but I think you have some architectural problems which will make it difficult to implement what I suggested above. In order for this to work and automatically update your controls whenever other controls' content change, your ViewModel needs to implement INotifyPropertyChanged and raise PropertyChanged event every time you update one of those boolean properties.
I think what you're trying to do could be achieved with ItemsControl and DataTemplates (and maybe DataTemplateSelectors). This will allow you to store "data" in your ViewModel (say List or something more specific) without referencing the actual Controls and the relevant DataTemplates will add the right controls for different data types you have in your ViewModel.
I have a DevExpress GridControl bound to a BindingList. I used a BindingList so that changes are automatically applied to the bound control.
It works perfectly when I add or remove items from the list, but when I change an item, it doesn't quit work the way I want it.
I created a foreach loop that runs in another thread (to keep the UI thread free to update) that iterates thru all the objects in the BindingList and changes some of their properties.
The way I expect it to work is that each property updated updates on the GridControl in real time. However the updating is very "chunky". Sometimes it updates 2 rows at a time, sometimes I have to click the GridControl to get the new values.
Why does this happen?
Is it a good solution to call DataControl.RefreshDataSource() after each item?
but when I change an item, it doesn't quit work the way I want it.
Becasue changes within an item are not somethign the BindingList cares about -that is why the items should implement INotifyPropertyChanged.
Basically the binding list says when the LIST has changed, but if you update a property - how should the list know ;) And why should it care - every item is separately responsible for publishing updates to its properties using said interface.
Is it a good solution to call DataControl.RefreshDataSource() after each item?
Worst way. Make sure the items implement INotifyPropertyChange and raise the PropertyChanged event accordingly.
The objects in your BindingList should implement the INotifyPropertyChanged interface and raise the PropertyChanged event when the value of their properties change.
I'm trying to create a binding from my custom control to objects that are in a BindingList.
While with textbox, I can easily write
textBox.DataBindings.Add("Text",myBindingList,"PropertyOfObjectOfBindingList")
With my custom property "Value", this thing doesn't work (the object doesn't get updated).
What should I implement with my custom control to make it works? I already implemented INotifyPropertyChanged, but it doesn't work.
I just want make this line works:
customControl.DataBindings.Add("CustomProperty",myBindingList,"PropertyOfObjectOfBindingList")
EDIT 1:
I read this around web: http://kbalertz.com/327413/control-using-Visual.aspx however is not working for me at the moment, maybe I'm doing something wrong
Since you said your bound object doesn't get updated (I assume from Control -> Object changes), but it is bound correctly, maybe this will help:
customControl.DataBindings.Add("CustomProperty", list, "BoundObjectProperty",
false, DataSourceUpdateMode.OnPropertyChanged);
Maybe the Implementing complex data binding in custom controls article will help.
I solved the problem by myself:
While the article I linked is a good suggestion, there is a wrong part; you don't have to create an event in your custom class with PropertyChangedEventHandler, but just with EventHandler.
public event EventHandler CustomPropertyChanged;
Is enough to make everything works. Obviusly you have to call it when your property changes
EDIT 1:
I discovered a bad thing, while on textboxes, if the control lose focus the bindinglist get updated, on my custom controls this thing happens only when I change selected item in listbox.
I don't find a way to solve this at the moment.
I'm working on a project that controls multiple devices over USB and intercepts the WM_DEVICECHANGE events from Windows to manage currently connected devices and plugin modules using those devices. There exists a combobox on the main form that (should) display all available devices (the custom object) and be dynamically updated in the event of either device arrival or removal.
Now, every tutorial that I have read including some from here have directed me to set the DataSource property of the combobox to the List of objects that I have, and leave the DisplayMember/ValueMember property blank to display ToString() and to return the object as the value.
In some instances I have tried (such as post instantiating the List and populating it with sample/real objects) the combobox populates, however upon removal or arrival, the collection in the combobox does not update even when reassigning the List to the combobox's DataSource property after every device arrival/removal method.
EDIT: Oh right... the question...
How do I databind the collection of (custom objects returned as the values) dynamically such that it can handle sudden changes (additions/removals) to the list's contents?
EDIT 2: Sorry I didn't make this more clear, wasn't aware of how pervasive WPF has gotten, but this is a WinForms project.
Thanks in advance for any help you can provide. :)
even when reassigning the List to the
combobox's DataSource property after
every device arrival/removal method.
After reassigning, you need to call .DataBind() again.
If you're using .net 3.5 or above, you can use an ObservableCollection to have the list update automagically.
I'm not sure if you're using WinForms or WPF. I'm assuming WPF as nobody should start out on WinForms nowadays...
You need to change your List to an ObservableCollection<T>.
The WPF binding model relies heavily on a magic subsystem of fairies and warlocks (not mocking you, I'm actually serious). They have their own language for informing each other that something has changed between the DataContext and UI. These are primarily the DependencyObject/Dependency properties, INotifyPropertyChanged and INotifyCollectionChanged, and the ObservableCollection<T>.
By proper use of these totems, changes in your ViewModels/Models (the stuff you stick in the DataContext and/or bind to the DataSource) will be reflected on the UI.
I don't know about that ToString() bit. I always set the DisplayMember to the Property I want to display. I don't care about the ValueMember because the SelectedItem is my Object.
DataBind doesn't apply if this is a WinForms project, which it sounds like. (USB and stuff)
I don't do anything funky in my project when up update the data in a ComboBox, I just set the DataSource again.
EDIT:
You could try using a BindingList rather than a List also.
http://msdn.microsoft.com/en-us/library/ms132679.aspx
How is data binding in C# WinForms supposed to work when you have a Save button? I don't want the data updated until I press Save!
I have two forms (list and detail) backed by a BindingList<T> collection and my custom object from that collection, respectively. I can bind each form to the list or object appropriately. However, any changes made in the detail form are immediately reflected in the list form - I don't want to save the changes and update the details shown in the list until the Save button is pressed.
Is data binding designed to support this? Is there a common pattern for doing so?
Whichever way I look at it, binding doesn't seem to be able to support this scenario. I've considered the following:
Pass a clone of the object to the detail form, but then I have to reconcile the changes on Save - changes may have been made to the copy in the list in the meantime.
Implementing IEditableObject and calling EndEdit on save almost works as I can prevent the list being notified of the changes made until Save is pressed, but if something else causes a refresh the list is updated with the interim data.
I'm currently left with dispensing with data binding in my detail view, and doing it all manually. Which is rather annoying.
Data binding really wasn't designed for this kind of thing, unfortunately.
The best solution we've found here is to edit object clones, then reconcile the changes, as you mentioned. You may want to come up with a custom collection and/or object interface that helps you manage this type of thing.
Even if the cloning method is slightly more work up front, it's going to be wayyyy less complicated and frustrating than using IEditableObject trying to catch all the possible events that update the data. Not only that, it's a more straightforward approach, and you won't end up with spaghetti code.
If you are set on using a binding list, your best bet would be to implement IBindingList to create the functionality that you desire. It may also be possible to pull this off by simply inheriting from BindingList and overriding the appropriate methods to change the binding list's behavior.
http://msdn.microsoft.com/en-us/library/system.componentmodel.ibindinglist.aspx
If you are not set on using a binding list, it is probably best to do the data manipulations manually based off of the control's events.
Best of luck.