Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
345
XamMenuItem Command Binding Dynamically and Dynamic Styling
posted

Hi Team,

I added added a XamMenu to my page. I hen added in Xaml few Menu items. One of them has sub items which are bound to ObservableCollection of type string. 

<ig:XamMenuItem x:Name="openRecent" Header="Open Recent" 
                Style="{StaticResource DarkXamMenu}"
                ItemsSource="{Binding RecentWells}">
</ig:XamMenuItem>

I encountered two problems:

1. The sub menu items do not inherit the style that I applied to the parent menu item. How do I apply the same style to the sub items that are generated dynamically?

2. I'd like the click event of these submenu items to be bound to a command in a view model. Any new item that will be generated based on the changes in the Observable collection should trigger this command and pass the text of the item (header?) as an argument.

This is basically a try to create an "Open Recent File" menu item where recent file names will be shown to the user...

How do I go about these two problems?

Thanks

Michal
Parents
  • 345
    Verified Answer
    Offline posted

    I think I can now answer my Own Question. 
    I now do not bind the ItemsSource to ObservableCollection but rather create the XamMenuItem(s) during Loaded event of the control:

    this.openRecent.Items.Clear(); //ManuItem creted in xaml and given name
    foreach (var well in _vm.RecentWells) //_vm is this.DataContext as MyViewModel and RecentWells 
    //is my ObservableCollection {     var newItem = new XamMenuItem();     newItem.Header = well;     newItem.Click += _vm.OpenRecent;     newItem.Style = FindResource("XamMenuItemStyle"as Style;     this.openRecent.Items.Add(newItem); }

    This way I bind each of the items Click event to a void in a ViewModel.
    At the same time I bind the style to a style that I created in StaticResources.
    Hope it will by helpful to someone else
Reply Children
No Data