Stéphan F
Stéphan F

Reputation: 95

Caliburn.Micro Add Dynamic MenuItem

I'm using Caliburn.Micro I try do create dynamic ItemMenu in a existing Menu. In the View "MainView.xaml" I put

<Menu Grid.Row="0" Grid.Column="1" Grid.ColumnSpan="4" Margin="0">
        <MenuItem Header="_File">
            <MenuItem Header="_Open" Name="FileOpen"/>
            <ContentControl x:Name="MenuItems"  />
            <Separator/>
            <ItemsControl ItemsSource="{Binding RFL}"/>
            <!--<common:RecentFileList  x:Name="RecentFileList">
            </common:RecentFileList>-->
        </MenuItem>

In the ViewModel "MainViewModel" I have this

       public BindableCollection<MenuItem> MenuItems
    {
        get { return menuItems; }
        set { menuItems = value; }
    }


    public void MenuItem_New()
    { 

    }
    public MainViewModel()
    {
        recentFileList.MenuClick += (s, e) => SendPathToUserControls(e.Filepath);

        MenuItems = new BindableCollection<MenuItem>();

        this._menu = new MenuViewModel();


        MenuItem menuItem = new MenuItem( );
        menuItem.Header = "Sous Menu 1";
        menuItem.Name = "SousMenu1";
        MenuItems.Add(menuItem);
    }

And when I execute I have this message :

enter image description here

How can I create dynamic MenuItemg without having to do a View And subsidiary question, how to raise the command with a parameter that contains the text of the menuitem (I'm trying to do a Most Recent File)

Upvotes: 2

Views: 213

Answers (1)

Frenchy
Frenchy

Reputation: 17007

For dynamic Menu You could use CompositeCollection

For the RecentList of files , i have a sample

           <MenuItem x:Name="RecentFiles" Header="Recent _Files" cal:Message.Attach="OpenRecentFile($srccontext)">
                <MenuItem.Icon>
                    <Image Source="{StaticResource IconOpen}"/>
                </MenuItem.Icon>
                <MenuItem.ItemTemplate>
                    <DataTemplate>
                        <Label Content="{Binding Caption}"/>
                    </DataTemplate>
                </MenuItem.ItemTemplate>
            </MenuItem>

You create a custombinding by calling this method from bootstrapper See that

    private void SetupCustomMessageBindings()
    {
        MessageBinder.SpecialValues.Add("$srccontext", context =>
        {
            var args = context.EventArgs as RoutedEventArgs;
            if (args == null)
            {
                return null;
            }

            var fe = args.OriginalSource as FrameworkElement;
            if (fe == null)
            {
                return null;
            }

            return fe.DataContext;
        });            
    }

You create a new class for example

public class RecentFileViewModel
{
    private int index;
    private const string shortcutKey = "_";

    public RecentFileViewModel(string file, int index)
    {
        File = file;
        this.index = index++;
        Caption = string.Format("{0} {1}", GetShortcut(), file.Replace(shortcutKey, "__"));
    }

    private string GetShortcut()
    {
        var str = index.ToString();
        if (str.Length == 1) return shortcutKey + str;

        return str.Substring(0, str.Length - 1) + shortcutKey + str.Substring(str.Length - 1);
    }

    public string File { get; private set; }

    public string Caption { get; private set; }


}

in your mainMenuviewmodel (for example), you have: (name convention xith Caliburn)

    RecentFiles = new BindableCollection<RecentFileViewModel>(ListRecentFiles());
    //you manage your ListRecentFiles

    public bool CanOpenRecentFile
    {
        get { return Recentfiles.Any(); }
    }

    public void OpenRecentfile(RecentFileViewModel recentfile)
    {
        //todo

Upvotes: 3

Related Questions