how can set a property when change one of the many conditions? - wpf

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.

Related

wpf combobox prevent text changed

is there any way to prevent wpf combobox from changing its text after selection changed? I have a custom control that derives from combobox and I want to be able to set the text manually after selection changed, additionally I cannot prevent the base.OnSelectionChanged from being invoked (this does the trick but it has to stay there as a part of requirements)
In general the IsEditable and the IsReadOnly properties of ComboBox are used to control the level to which the display Text of the ComboBox is editable or selectable by the user.
In the msdn combobox (section remarks) you can read about it.
I had a similar issue to solve, here's how I did it:
My First ComboBox item is an object implementing NotifiyPropertyChanged, i can change its value at any time and it updates.
I put its IsEnabled to False so that the user cannot select it.
If you want this item to be displayed the same way as others even when disabled, design your ItemTemplate.
In the SelectionChanged handler, if the selected index 0, I do nothing.
If the selectedIndex is not the first, I do my computation with this index (including updating the first item's text) then I set SelectedIndex to 0.
Edit 2 : try to set the grid's IsHitTestVisible to False, and to True for the CheckBoxes.
Edit 1 : If the first solution doesn't work : So the core issue is that when you click on a row and not on a CheckBox, it triggers SelectionChange. What you have to do is to handle the tunnelling left click event : Add a handler (in xaml more simple than in code) to PreviewMouseLeftButtonDown, and in the handler get the OriginalSource of the MouseButtonEventArgs. First Check that we are in second choice (index:1) of the CheckBox by checking if the Original source or one of its visual parent is the the second CheckBoxItem. If its not then return. Now if the OriginalSource is a CheckBox or is a visual parent a CheckBox then do nothing Otherwise mark the event as handled.
NB : You'll have to use VisualTreeHelper.GetParent and write a sub that checks if a Dependency object or one of its parent is of a given type. (the top most parent is the Window, having Nothing/Null as parent.) This sub will return the right typed object if found, or Noting/Null if not found.

Binding is not updating multiple UI elements

I have buttons on my Silverlight page where the opacity is bound to one of two properties on my ViewModel. I'm using the button command that changes the properties, in theory to affect all buttons bound to that property, but the only control that gets affected is the button that initiates the command (any one of them).
Any ideas on why the additional bindings don't work?
The whole thing is actually a little more complex where the buttons are on a Control with the bindings as DependencyProperties mapping back to the VM, and the bound properties are going through a ValueConverter.
It sounds like you need to raise the INotifyPropertyChanged.PropertyChanged event for the properties that are changing. This will let the controls that are bound to them know that there is a change and that they need to come back and get the latest value.
I found the problem. The opacity binding wasn't working, but what was happening was the button was disabling itself based on the predicate I had set in the RelayCommand. It looked like the effect I wanted, but only affected the button being pressed because each button was bound to a seperate ICommand.
I changed it to remove change the binding from the OpacityProperty to the IsEnabledProperty, and removed the predicate from the RelayCommand declaration. It all works now as intended.
I don't know why the button would change to a disabled view when it checks the predicate (and finds it false), but never change back if the condition changes. Odd.

WPF, Binding a combobox and textbox to each other AND data source

I have a two column combobox, and a textbox, bound to xml data.
The textbox shows the equivalent of the comboboxes second column of the currently selected item.
I've bound the datacontext of the textbox to the SelectedItem in the combobox, which then updates if you select a row in the combobox. Now, I'd it so that if you type something into the textbox that corresponds to a value in the 2nd column of the combobox, it selects that row.
I realise this is slightly circular.
I've managed it before in winforms, by effectively suspending events when the CombobBox OnSelectedItemChanged fires and updates the textbox or OnTextChange fires and updates selectedItem.
The idea is that the user can either select an option from the combo, or if they know a short code (in this case, country ISO), they can just type it in and immediately see the appropriate country selected in the combobox.
Is it somehow possible to bind the selectedItem in the combobox to the textBox in addition to the underlying data (and indeed does that idea make any sense?), or possibly do some sort of two-way-binding between these elements?
I'm hoping there's a simpler solution than dependencyproperties-- ideally something purely in xaml, but appreciate as I'm new at WPF, I've no idea if this is even possible.
Thanks!
Mike
If you've got a ViewModel you can two-way bind each to the same property of that, and this is probably the best way to do it.

Wpf combobox text property binding 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.

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.

Resources