0
votes

I'm tring to take the best from Catel, I was refactoring my old code using service pattern and so on ...

I've created a view called MenuView

<catel:UserControl x:Class="CatelDocking.Menu.Views.MenuView"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               xmlns:catel="http://catel.codeplex.com">

<telerik:RadMenu ItemsSource="{Binding Menus}"></telerik:RadMenu>

inserted into a mainView

 <Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="30" ></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <view:MenuView ></view:MenuView>
</Grid>

and their respective viewmodels:

 public class MenuViewModel : ViewModelBase
{

    private readonly IMenuService menuService;

    public MenuViewModel(IMenuService menuService)
    {
        Argument.IsNotNull(() => menuService);

        this.menuService = menuService;
    }

    protected override System.Threading.Tasks.Task Initialize()
    {
        menuService.AddTopLevelMenu(new Models.MenuItemNode { Text = "Test" });
        return base.Initialize();
    }
}

public class MainWindowViewModel : ViewModelBase { private readonly MenuViewModel menuViewModel;

    public MainWindowViewModel(MenuViewModel menuViewModel)
    {
        this.menuViewModel = menuViewModel;
    }

    public override string Title { get { return "CatelDocking.Menu"; } }

    /// <summary>
    /// The Menu view model.
    /// </summary>
    public MenuViewModel MenuViewModel
    {
        get { return GetValue<MenuViewModel>(MenuViewModelProperty); }
        set { SetValue(MenuViewModelProperty, value); }
    }

    /// <summary>
    /// Register the menuViewModel property so it is known in the class.
    /// </summary>
    public static readonly PropertyData MenuViewModelProperty = RegisterProperty("MenuViewModel", typeof(MenuViewModel), null);
}

where IMenuService is defined as

public interface IMenuService
{
    MainMenuNode GetMenuRoot();
    void AddTopLevelMenu(MenuItemNode node);
    void RegisterMenu(MenuItemNode node);
}

In order to have things at the right place, should I have a MenuViewModel in the MainViewModel? if not so Catel is View-First and not ViewModel-First (that's no a problem, just to know it!)

With this approach the best Menus binding should be done in the MenuViewModel is it right?

I've attached a common command to open the selected view from the menu...was wondering what's the best approach in the command code to send information to the shwllviewmodel/idockingservice...

Should I use a message based or have I to keep a reference to she shellviewmodel / idockingservice then call a method defined in.

thanks

1

1 Answers

1
votes
  1. Use async await in your initialize method:

protected override async Task Initialize.

  1. Use Catel.Fody, then you can easily use simple properties which are automatically converted to Catel properties on Catel classes

  2. No, you don't need the MenuViewModel in the MainViewModel. The MenuView / MenuViewModel can be self-providing so it can run on it's own and placed anywhere. The whole point of Catel is to prevent people having to mess around with the manual view model creation everyone is doing. You can just put the datacontext on a view, and it should create the right vm on its own.