.netwpfribbonmru

Click event in RibbonGalleryCategory inside AuxiliaryPaneContent?


I have been following the example on the bottom of this page:

http://msdn.microsoft.com/en-us/library/microsoft.windows.controls.ribbon.ribbonapplicationmenu.auxiliarypanecontent.aspx

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?


Solution

  • 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);
        }
    }