I have been following the example on the bottom of this page:
to get a "Most recent documents" list. I have the list populated and I can click on the items in this list but I can't find where to catch the click event.
I need to know when and on what item the user has clicked on in this list.
How?
There are two ways you can solve it.
First: Use Ribbon.SelectionChanged event. It will catch your ListBox SelectionChanged event too and you can add your logic to it.
private void RibbonSelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.OriginalSource is Ribbon)
{
//implement your logic
}
if (e.OriginalSource is ListBox)
{
//implement your logic
}
}
Second: I prefer to use ListView but I think its the same in this case. Create your custom ListBox with Click event.
public class RecentItemsList : System.Windows.Controls.ListView
{
public delegate void RecentItemClicked(object param);
public event RecentItemClicked Click;
public RecentItemsList()
{
SelectionChanged += RecentItemsList_SelectionChanged;
SetValue(ScrollViewer.HorizontalScrollBarVisibilityProperty, ScrollBarVisibility.Hidden);
//...
}
private void RecentItemsList_SelectionChanged(object sender, SelectionChangedEventArgs selectionChangedEventArgs)
{
if (SelectedIndex > -1)
{
//...
OnClick();
}
}
private void OnClick()
{
if (Click != null)
Click(null);
}
}