wpf combobox prevent text changed - wpf

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.

Related

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

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 to set focus to UserControl (make it selectable)?

I need to set focus to UserControl itself, not its child.
Otherwise I cannot implement insertion from the buffer. :(
Setting Focusable=True doesn't help.
Google and SO tells only how to set focus to UserControl child.
My control contains:
- Toolbar with several buttons bound to commands of the corresponding
VM
- TextBox which is the input for the filter
- DataGrid - list of items.
I need to bind Ctrl+V command to VM. But to handle this gesture UserControl must have focus within. When there are no items in the grid (VM's collection is empty) buttons are disabled and the only element which can get focus is TextBox. But it handles Ctrl+V in its own way and I don't want to change this behavior.
Thus, I need something to set focus to when I click the area of UserControl.
I believe UserControl is the best candidate for it.
But I don't know how to make it selectable.
The whole problem was in my misunderstanding of controls' behavior.
This SO question clearly shows it I believe.
Thus, setting UserControl.Focusable = true is not sufficient. To make it navigatable via keyboard IsTabStop must be true also. And to make UC selectable by mouse click we should call Focus() in mouse eventhandler. That's it.

wpf - autocompletebox doesn't update on mousedown

I have an autocompletebox that works but for one oddity I was hoping for help with. When selecting an item in the popup, using the keyboard to arrow down and then selecting it using either the enter key or with the mouse, the item is selected and updated into the autocompletebox. However, if one instead of arrowing down to the item simply hovers over it instead and select it with the mouse, the selecteditem is set correctly but the autocompletebox doesn't get updated with the selecteditem.
That is, arrow down and select an item and the autocompletebox reflects the selected item whereas just hovering over and selecting the item means the autocompletebox does not reflect the selection - instead it shows what the user typed into the box; note that the backing property is aware of the selected item so it does work either way, only it's not reflected in the textbox.
Any help is appreciated.
Thanks
I should add that it is OnSelectedItemChanged that doesn't seem to get called...
This issue is explained here: http://www.siimviikman.com/2012/05/30/wpf-autocompleteboxfiltering-similar-items/
As precised in the end of the article, the user cannot navigate through items (with arrow keys + hit TAB). That is why I could not use their solution.
In the WPF Toolkit source code (UpdateTextCompletion method), one can read:
// Perform an exact string lookup for the text. This is a
// design change from the original Toolkit release when the
// IsTextCompletionEnabled property behaved just like the
// WPF ComboBox's IsTextSearchEnabled property.
//
// This change provides the behavior that most people expect
// to find: a lookup for the value is always performed.
newSelectedItem = TryGetMatch(text, _view, AutoCompleteSearch.GetFilter(AutoCompleteFilterMode.EqualsCaseSensitive));
So I simply patched the WPF Toolkit and commented out the calls to UpdateTextCompletion method, both in OnAdapterSelectionComplete and OnAdapterSelectionCanceled.
As I don't use text completion, this fix seems to work fine.

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

Resources