WPF ComboBox DataBound Event? - wpf

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

Related

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: Is there a "BeforeSelectionChanged" event for the combobox?

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.

Accessing controls from within commands in WPF

I've got a WPF app who's menu is using the commanding capabilities. Everything is wired up just fine and when I click the buttons in the menu, the commands run. However I'm having trouble getting the button's IsEnabled status to respect the CanExecute part of my commands.
One challenge is the commands need to see what you're doing in the UI. For instance, one command should only be available when certain items in a ListBox are selected so I need to get a reference to the ListBox... but since the command is exposed in my view model (MVVM pattern), it doesn't have access to the UI (BTW, the menu is in one user control [parent=mainwindow] while the ListBox is in another user control [parent=mainwindow]).
In addition, even when I hard code the command's CanExecute method to return false, the Enabled property of the button doesn't change... the command won't fire, but it won't change... frustrating. I assume I need to modify/raise the CanExecuteChanged event, but I'm not sure when I should be doing that (can't find a good sample).
Feedback?
Try the Messenger class from MVVMLight. It helps in communicating between ViewModels.
And give this a try:
Have a SelectedItem property in your ListBox's ViewModel. Broadcast messages while the property changes. Register for this message in the menu's ViewModel. Use the SelectedItem property in your CanExecute method for your logic.
Normally, you would bind the Command Property of the MenuItem/Button whatever - that means you still have the CommandParameter to work with - this can then be bound to some other control. However, when the two views do not know each-other, you need som kind of mediator between them (ie. a viewmodel that both views can access - have the listbox SelectedItem/SelectedItems bound to a property two-ways - and let the CommandParameter bind to the same property one-way).
As for when to fire the CanExecuteChanged event - you should fire that whenever there is a change in the logic contained in the CanExecute-method. If it is always false, you should never fire the event, as it will read the initalvalue when the Command-parameter is set. '
If your button is behaving oddly - check to see if the IsEnabled property is influenced by Styles or set directly.
Hope this helps.

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/

Where is DataGrid RowDeleted event?

Hello I am looking for an event I can handle for row deletion.
Instead of using an event on the DataGrid you should use a collection as its ItemsSource which supports change notification. Unfortunately the de-facto standard "ObservableCollection" does not support an event that fires before an item is removed, but it's quite easy to derive your own collection class from ObservableCollection which overrides RemoveItem and raises an appropriate event that can be cancelled.

Resources