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?
Related
I have a button that I want to disabled in some cases. To determinate that, I use the selection of many controls in the view. For simplify in this question, two comboBox.
So the IsEnabled depends on the combiation of the information in this two controls, I need to evaluate the new state when change one of them. How can I do that? I know that I need a multi value converter to determinate if the button is enabled or not, but I don't know how to execute the converter when the selection in one of the combobox is changed.
EDIT:
When I have said before Multi value converter I wanted to say multi binding.
Perhaps I have not been very clear. I want the following:
1.- In the beginning the button is disabled and the two comboBox have not any item selected.
2.- When I selected an item in one of the comboBox, I need to execute the multi binding that is used to set the IsEnabled property of the button.
And repeat the process when I selected a new item in any of the comboBox.
The problem that I have is that I don't know how to say to the button that when I change the selection in any of the comboBox, the button need to execute the multi binding to determinate the value of the IsEnabled property.
but I don't know how to execute the converter when the selection in one of the combobox is changed.
As long as the values you're binding to are either DependencyProperty values or part of a class that (properly) implements INotifyPropertyChanged, this will happen automatically. You shouldn't need to do anything to update the values.
Just make sure the bound values notify as if they were used directly, and WPF will handle this when using an IMultiValueConverter just fine.
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.
I am trying to bind to a combobox text with the IsEditable property set to true. I have a property in my viewmodel which is bound to the text.
I want to validate on the text being typed in the text of the combobox, and restrict some values that the user is typing in. So some will be allowed, and some not, and these need to set the combobox back to its old value.
I do this in the view model and I have tried setting my text property in my view model explicity to the old value or just ignoring the change and raising that the property has been changed, but for the life of me it will not refresh the text back to the old value.
Is this because the combobox is editable, and it has the text caret and focus somewhere in the text of the combobox.
Basically, I want it to refresh back to the previous text when I restrict some typing in the combobox during in editing. Anyone have any ideas to reset the text back to its old value through the ViewModel. Thanks in advance!
Thanks for your replies. But I could never get it to work instead, I made my own UserControl which comprises a textbox overlayed over a combobox, and manipulate those two controls to meet my needs. A long way to go to solve a simple problem, but it works in the end.
Is the viewmodel property you are binding to created as a DependencyProperty? This is probably the problem you are facing Two-way binding in WPF
If you don't want to create a Dependency property then you need to implement INotifyProperty changed and manually force the update in the Property changed event.
I think this is because of a 'bug' in WPF not refreshing the UI if you change the value of a property in the setter. You can workaround it by implementing an IdentityConverter that force the UI to refresh as per this arcticle.
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.
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.