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.
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.
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?
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 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.
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.