WPF: Is there a "BeforeSelectionChanged" event for the combobox? - wpf

Is there a "BeforeSelectionChanged" event for the combobox? I want to verify some stuff before the SelectedItem property changes.

There's no PreviewSelectionChanged event. Instead of using two way binding, use one way binding to SelectedItem and get updates through command or SelectionChanged event. That way you can in the handler do some verification and even fake a cancel of the selection.

I don't think that there is, unfortunately.
You might be able to use the PreviewLeftMouseDown event and determine if the mouse is over an item in the ComboBox. If it is over an item that isn't the SelectedItem, you know it is about to change.

Related

How can I prevent a dropdown from closing when I`ve selected an item?

I have custom control, derived from combobox.
I want it to not close itself when I select an item. If I set the IsDropDownOpen property to false in protected override void OnDropDownClosed(EventArgs e), this method just starts to chain invoke itself.
If IsDropDownOpen is set in the OnSelectionChanged handler nothing happens.
Any suggestions?
See this answer -- https://stackoverflow.com/a/22814332/1547004
Essentially, install an eventFilter on the ComboBox's listview to catch the MouseButtonPress event to prevent it from passing through and causing the listview to close.
No way. ComboBox is ComboBox, when a item is selected automatically closes it. ListBox is a good option here.
Regards.

wpf: TextChanged event fired on setting DataContext

I've got a simple View with a single textbox that gets databound to a simple ViewModel with a single string property.
I need to catch the TextChanged event of that textbox so that I can do a little validation magic.
The problem that I am running into is that the TextChanged event fires for that textbox when the DataContext is set for the View.
Is there a standard mechanism that I can use to determine if the event is firing because of the DataContext being set versus when the user is making changes?
Thanks!
As far as I know there is no such mechanism. What you should do instead is to do your validation magic using standard means of WPF. Please see the following link: http://msdn.microsoft.com/en-us/library/ms752347.aspx#data_validation.
Anyway, as long as you use MVVM you can always detect that text has changed in the setter of the bound property in your view model.

WPF ComboBox DataBound Event?

I use ComboBox.ItemsSource=[some data collection] to bind data to the control.
I want to hookup an event handler to the combobox so that whenever its data updated (or first time bound), I can do somthing.
The problem is I can't find an appropriate event for it. The close guess is DataContextChanged. But that is not invoked when the items get bound/created.
Many thanks in advance for any helps.
Cheers~
The ComboBox.Items property is of type ItemCollection, which has CollectionChanged, CurrentChanged, CurrentChanging events. They should suit your needs.
ItemCollection Class MSDN Article

ListView.SelectionChanged to RoutedCommand

I'm working in the MVVM design pattern with WPF. I have a ContextMenu with several items in it on a ListView. Based on the number of items selected in the ListView, I want to enable/disable certain MenuItems. Is there a way to route the SelectionChanged event along with the number of selected items in the ListView directly to the view model. If so, I can define a dependency property in the VM for IsEnabled quite easily. I'm just trying to avoid code-behind to handle this.
Kelly
You can use an attached behavior to route the SelectionChanged event to your VM. Basically, you create an attached property of type bool. When this property is set to true, you register an event handler for the SelectionChanged event of the target Menu.
Then an attached property can contains the command to execute (databound to a RelayCommand-like command in your VM).
Check those posts for more details:
http://www.japf.fr/2008/12/how-to-attach-commands-to-any-uielement/
http://marlongrech.wordpress.com/2008/12/13/attachedcommandbehavior-v2-aka-acb/

WPF: Detect when selected item changes

I have a control that is data-bound to a ListBox. All of the bound properties are being updated correctly. However, the control needs to know when the selected item changes so that it can do some other cleanup. Is there an event that covers this?
You can also bind to the SelectedItem property, say with ICollectionView.CurrentItem, and set the IsSynchronizedWithCurrentItem property to True.
There is SelectionChanged event in ListBox.

Resources