Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ViewModels collection for Conductor<IScreen>.Collection.OneActive #21

Closed
Splinterjke opened this issue Dec 18, 2016 · 3 comments
Closed

Comments

@Splinterjke
Copy link

Splinterjke commented Dec 18, 2016

Is there any the fastest way to set overrided IModelValidator for all ViewModels and add all ViewModels in some ShellViewModel not like this default way:

public MainViewModel(SomeViewModel tab1, AnotherViewModel tab2, /*etc*/) { //Adding items }

and not with (for every usercontrol viewmodel):

public SomeViewModel(IModelValidator<SomeViewModel> validator) : base(validator) { //smthg }

I mean the situation when there is alot of UserControls in Project.

@Splinterjke
Copy link
Author

Got it from Caliburn.Micro like this:
Main ViewModel

public class MainViewModel : Conductor<IScreen>.Collection.OneActive
{
    public MainViewModel(IEnumerable<TabItem> tabs)
    {
        DisplayName = "Name";						
        Items.AddRange(tabs);
    }
}

Prepared a marker interface for all child items

public interface TabItem: IScreen
{
}

Registering and binding interface to viewmodels

protected override void ConfigureIoC(IStyletIoCBuilder builder)
{
     builder.Bind(typeof(TabItem)).ToAllImplementations();
}

@canton7
Copy link
Owner

canton7 commented Jan 3, 2017

Hi,

Sorry for the delay - Christmas and New Year happened.

Is your question resolved?

Note that, in your second post, the order of tabs isn't guaranteed: it may changed based on random stuff.

@Splinterjke
Copy link
Author

You're absolutely right, the tabs will be placed in the same order the classes are injected, but I have custom sorting for that. If someone wants to have a control over the ordering, you can order them in 'MainViewModel' constructor by either passing a custom 'IComparer' or adding some property you can OrderBy or select to the ITabItem interface. Also you can use IList methods to sort items. The default selected item will be the first one in the Items list (the same as in caliburn.micro tho). Question is resolved ofc.

@canton7 canton7 closed this as completed Feb 25, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants