Force Binding Update Silverlight - silverlight

How can I force my objects DataContext bindings to update? I'm using an event on a grid, and binding updates are not being processed before my event fires.
Any cheap tricks to get around this?
In the end I can always do things the old manual way of getting the values from my textboxes and updating my object, but it'd be nice to have binding do it for me.
UPDATE
My grid contains two textboxes. If a user clicks on the grid (MouseButtonUp event) then I save the changes. But in my MouseButtonUp event handler, the datacontext is not up to date yet. I'd imagine it's because a text box only updates when focus is lost.

You can force an update of the source a binding by calling the UpdateSource() methond on on the binding. Try adding it to the MouseButtonUp event handler just before saving.
Like so:
BindingExpression binding = FirstTextBox.GetBindingExpression(TextBox.TextProperty)
if (null != binding) binding.UpdateSource();
You can find more information about this on MSDN: http://msdn.microsoft.com/en-us/library/system.windows.data.bindingexpression.updatesource(VS.95).aspx

Alternativly you can catch TextInput event (it will fire when user presses enter inside text box)
and then change focus to second textBox. loosing focus will update binded value.

Related

Need Validate All Properties in Class with IDataErrorInfo Implemented

I have a WPF window that has a datacontext of my class 'Item'. When a user types in a TextBox the validation triggers with no issues. I need to validate this TextBox and every other Property that is set in this bound class when the user clicks 'Save'.
I don't believe UpdateSourceTrigger is what I'm looking for, although I have it set to PropertyChanged for when the user does actually do data entry on the field.
I would rather not set all my properties to the corresponding UI control on the Save click to force the update on the property but I could.
Thanks!
Data error validation only gets executed when binding pushes new property values out to the data source. If you want to execute that validation under any other circumstances, you have to write code to do it.
If your UI properties are bound to the item's properties, then the validation already should have run for each of them when the Save button is clicked. So why do you need to run it again?
One common issue is where you've set your bindings to update on LostFocus, but the object that the user clicks on to save the item isn't focusable. In this case, the last property updated in the UI isn't updated in the data source when the item is saved, since its control hasn't lost focus and its binding hasn't fired. Is that your problem?

WPF - Temporarily Stop Data Binding (databinding)

I have a textbox that is bound (oneway) to a datatable that updates a couple of time a second.
So the textbox value continually reflects changes in datatable.
When I enter the textbox to manually set a value, the binding causes the value to continually be overwritten. How do I stop this?
When I have entered a value (textbox lost focus) I want the textbox to return to display the bound value and not the value I have just entered manually.
I had the same problem and I solve it by
With the BindingNavigator, I set it to null
bdNavProduct.BindingSource =null;
With other textbox control, datagridview, I clear the DataBinding
txtProductID.DataBindings.Clear();
txtProductName.DataBindings.Clear();
txtQuantity.DataBindings.Clear();
txtUnitPrice.DataBindings.Clear();
dgvProduct.DataBindings.Clear();
Tony
You could attach to the text input event and cancel the binding and then reapply it on the lost focus event.
I think however you need to consider why you are displaying your bound value in a text box at all? Would it not be more appropriate to have an uneditable Textblock displaying your database information with the editable Textbox separate for the optional user input.
While what you are asking is doable using the Textbox event I mentioned, it seems like it would be confusing from a user perspective.

How to change cell background in ListView when data changes?

I have ListView with binding on ObservableCollection which has INotifyPropertyChanged implemented. When user open window, data will be displayed but I want to change backgorund on cells where data was changed in meanwhile...
I have been trying with Data triggers, but they can be triggered just on certain values. I want to trigger when value changes, whatever value is.
Thanks
You can use DataTrigger for that. But you need to add corresponding IsDirty property to the ViewModel class and on the setter of your property you can check whether Data has been modified and fire the IsDirty. Which inturn fire DataTrigger and so the background.
For example if you got a FirstName property, you might need IsFirstNameDirty:bool There is no other easy way the WPF checks for your value changed from the intial one.

How can I prevent duplicates in a WPF Listbox control?

I have a WPF listbox control that is declaratively bound to a textbox. The listbox's ItemsSource is an ObservableCollection that is built from an XML file. I can easily prevent duplicate entries in the listbox when a new item is added because I can check for it in the "Add" button's Click event handler.
However, when an existing item's value is changed in the textbox (which obviously shows the listbox's selected item) to one that already exists in the list I want to prevent this, but I don't know how.
I'd appreciate help with this!
You can create your own validation rule by deriving from ValidationRule and apply it to your text box's binding. In the Validate method you can check for duplicates and return a ValidationResult of false to prevent the binding source from being updated.
Listen to the CollectionChanged event and check when the collection has been modified if there are any duplicates and remove them.
Also, you can take a look at this question and its' answer for an observable collection that also notifies you when its' items' properties change.
Edit:
If you don't want to use the collection I mentioned above, you can make sure your collection's items implement INotifyPropertyChanged and every time you add an item to the collection, listen to its PropertyChanged event. In the handler, you check if the property that changes is the one that is displayed in the ListBox and check if any other element has the same value of this property. If you find such an element, you either change the value of your property to its old value, or remove the element entirely, it depends on the logic of your application.

WPF listview databinding - How do I force update to underlying object without tabbing out?

I have a WPF ListView that is bound to a BindingList<T>. The binding works like a charm, but I have to tab out of the cell to get the bound property to update....this is an issue because most users don't tab out of the last column before clicking the save button.
How do I force the list view to "persist" the changes to the bound DataContext without doing something hackish.
Bindings in WPF have a property called "UpdateSourceTrigger" which tells the Binding when to update the thing the UI is bound to. By default, it's set to "LostFocus" for the Text property which is what you're most likely using.
Change the trigger to "PropertyChanged" in your binding like this:
Text="{Binding Foo,UpdateSourceTrigger=PropertyChanged}"
... and now the source "Foo" property will be updated as the Text changes in the UI.
There's also an "Explicit" setting for UpdateSourceTrigger which is handy if you need to hold off writing any changes to the source until, say, the user clicks the OK button.

Resources