How to work with this menu "Recent Documents"? Add list items and handle clicks.

What I have. XAML

<Ribbon.ApplicationMenu> <RibbonApplicationMenu> <RibbonApplicationMenu.AuxiliaryPaneContent> <RibbonGallery CanUserFilter="False" ScrollViewer.VerticalScrollBarVisibility="Auto" SelectionChanged="RibbonGallery_SelectionChanged"> <RibbonGalleryCategory x:Name="ribbonGallery" Header="Последние документы" Background="Transparent" ItemsSource="{Binding}"> <RibbonGalleryCategory.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Vertical" IsItemsHost="True"/> </ItemsPanelTemplate> </RibbonGalleryCategory.ItemsPanel> </RibbonGalleryCategory> </RibbonGallery> </RibbonApplicationMenu.AuxiliaryPaneContent> </RibbonApplicationMenu> </Ribbon.ApplicationMenu> 

Add a collection of items (Last opened documents) - test data

 private void Window_Loaded(object sender, RoutedEventArgs e) { var portObsCol = new ObservableCollection<RibbonGalleryItem>(); portObsCol.Add(new RibbonGalleryItem() { Content = "documentPath/name1", Name = "Name1" }); portObsCol.Add(new RibbonGalleryItem() { Content = "documentPath/name2", Name = "Name2" }); ribbonGallery.ItemsSource = portObsCol; } 

I receive: Menu - Ribbon ApplicationMenu

When clicking on an item

 private void RibbonGallery_SelectionChanged(object sender, RoutedPropertyChangedEventArgs<object> e) { RibbonGallery ribbonGallery = sender as RibbonGallery; MessageBox.Show(ribbonGallery.SelectedValue.ToString()); } 

I receive: Window messagebox

Hope there is a better way to handle click and bind? =))


From the MSDN example

I could not find how to bind a dynamic resource ({DynamicResource MostRecentFiles}) for menu items. How can this be implemented in code?

 <ribbon:RibbonGalleryCategory Header="Recent Documents" Background="Transparent" ItemsSource="{DynamicResource MostRecentFiles}"> <ribbon:RibbonGalleryCategory.ItemsPanel> <ItemsPanelTemplate> <StackPanel Orientation="Vertical" IsItemsHost="True"/> </ItemsPanelTemplate> </ribbon:RibbonGalleryCategory.ItemsPanel> </ribbon:RibbonGalleryCategory> 
  • Yes, there are better options. Learn MVVM and link via Binding to DataContext 's. - VladD
  • A dynamic resource binding is not necessary. The item list is not a UI resource, it is a VM. - VladD

0