WPF: Detect when selected item changes - wpf

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.

Related

When binding to a combobox SelectedItem the change is only notified on lost focus. How to notify when selection is changed?

I've bound it using
cmbPeriod.DataBindings.Add("SelectedItem", Presenter, "SelectedDate", true, DataSourceUpdateMode.OnPropertyChanged);
But it only fires to the bound model when I tab out of the control, I'd like it to fire the moment the users makes a new selection.
EDIT: Ok so I tried binding using SelectedValue instead and leaving the ValueMember as null. This had the effect of updating the source as soon as the combobox changes with the correct object, however now the combobox ignores updates from the source!!
I see it requesting the binding at runtime and my source property returns the correct object, which is the the same type the combobox will update the source with on change. Ugh! So close:(
cmbPeriod.DataBindings.Add("SelectedValue", Presenter, "SelectedDate", true, DataSourceUpdateMode.OnPropertyChanged);
binding to SelectedValue works on change

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.

How can I set the keyboard focus to the content of a ContentPresenter when it changes?

I have a ContentControl in my view which is databound to the CurrentItem property of my viewmodel. The objects that are exposed via CurrentItem each has its own DataTemplate.
When the CurrentItem property changes, the appropriate DataTemplate for that item is displayed, as it should be. However, I cannot find out a way to set the keyboard focus to the content of the DataTemplate.
Even if I manually set the keyboard focus to the DataTemplate, if the CurrentItem property changes (and a new Template is instantiated) the focus is lost (FocusManager.GetFocussedElement returns null).
How can I set the keyboard focus to the content of the ContentPresenter when it changes?
I believe you can use the LayoutUpdated event on either your ContentControl or the ContentPresenter. This should fire anytime the Content/ContentTemplate/etc changes.
Alternatively, you could derive a class from ContentControl then override the OnContentChanged, OnContentTemplateChanged, etc methods. Then you'd need to search down the visual tree and set the focus. You may need to use the Dispatcher to delay the focus setting code.

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/

How do I BringToView the latest item added to a bound ObservableCollection

I have a silverlight control (View) which displays a list of items in a specified property of the datacontext (viewmodel).
What I need is for the scrollviewer in my control to scroll to the top or bottom depending on where the latest item has been added to the list. (It'll always be either the beginning or the end of the list, I don't need to worry about middle of list insertions.)
In WPF i'd just use the DataContextChanged event to start listening to the viewmodel, but in silverlight that event is internal.
Any ideas on how to tackle this?
A good starting point is Attached Behaviors on CodeProject.
A useful behavior would watch the ListBox.ItemsSource and attach to the observable collection when set. On the collection changed event, use ListBox.ScrollIntoView to display the changed item.
I can't use the CollectionChangedEvent of ObservableCollection since I need the DataContextChanged event to get the DataContext which holds the Collection in the first place.
Would you not do this in the ViewModel?
Whatever ViewModel has the ObservableCollection, expose a property of type T named SelectedItem and whenever the ObservableCollection changes with a new item, the CollectionChanged event will allow you to set the SelectedItem property. Once this is done, wire up the SelectedItem in the control to this property on your ViewModel.
This will obviously only work with controls like ListBox where a SelectedItem property exists.
In place of DataContextChanged in WPF , you can use CollectionChanged event of ObservableCollection. In the collection changed you will get to know the NewItem Index.

Resources