Can anybody provide any simple working example of the Conductor<T>.Collection.AllActive usage?
Asked Answered
P

2

6

It's a bit strange, but I really can't find a working example anywhere.

By the way, I'm using a ViewModel-first approach (in WPF) if this is important.

Thank you in advance.

Peen answered 16/12, 2013 at 9:19 Comment(5)
What you want to achieve out of thatOrgulous
We want to load three Views with ViewModels into one "container".Peen
Answer below worked for you ?Orgulous
hi, the answer worked for you ?Orgulous
@MilanRaval Yes, it worked.Peen
C
14

If you have a look at the discussion here you will see that the intent of AllActive is to compose several Views/ViewModels into a containing ViewModel. Judging from your previous comments it seems as if this is what you were expecting but I figured I'd at least reference it here.

You then mention activating 3 different ViewModels at different regions of the View. The way I've handled this in the past is to have separate properties for binding/referencing the ViewModels in the View, and then just adding all of them to Items to get the Conductor behavior.

public sealed class MyViewModel : Conductor<Screen>.Collection.AllActive
{
    public MyViewModel(IMagicViewModelFactory factory)
    {
        FirstSubViewModel = factory.MagicallyGiveMeTheViewModelIWant();
        SecondSubViewModel = factory.MagicallyGiveMeTheViewModelIWant();
        ThirdSubViewModel = factory.MagicallyGiveMeTheViewModelIWant();

        Items.Add(FirstSubViewModel);
        Items.Add(SecondSubViewModel);
        Items.Add(ThirdSubViewModel);
    }

    public Screen FirstSubViewModel { get; private set; }
    public Screen SecondSubViewModel { get; private set; }
    public Screen ThirdSubViewModel { get; private set; }
}

And in MyView you would have something like this. Of course you could put these ContentControls wherever you want to in the view.

<StackPanel>
    <ContentControl x:Name="FirstSubViewModel" />
    <ContentControl x:Name="SecondSubViewModel" />
    <ContentControl x:Name="ThirdSubViewModel" />
</StackPanel>

Another common use for AllActive is when you have a list of items. But the items are complex enough to warrant having their own View/ViewModels that require activation. In that case you would not have to have separate properties for the views as you would just set the x:Name of the list control to Items.

Cordoba answered 7/2, 2014 at 23:43 Comment(0)
O
0

You can do implement like below, use TreeViewModel at the place of TabViewModel

ShellView

 <DockPanel>
        <Button x:Name="OpenTab"
                Content="Open Tab" 
                DockPanel.Dock="Top" />
        <TabControl x:Name="Items">
            <TabControl.ItemTemplate>
                <DataTemplate>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding DisplayName}" />
                        <Button Content="X"
                                cal:Message.Attach="DeactivateItem($dataContext, 'true')" />
                    </StackPanel>
                </DataTemplate>
            </TabControl.ItemTemplate>
        </TabControl>
    </DockPanel>

ViewModel

public class ShellViewModel : Conductor<IScreen>.Collection.AllActive {

            System.Collections.Generic.List<TabViewModel> tabViewModelCollection = new System.Collections.Generic.List<TabViewModel>();

            public void ActiveAllTab() {

                foreach (var tabViewModel in tabViewModelCollection)
                {
                    ActivateItem(tabViewModel);
                }           
            }
        }
Orgulous answered 4/2, 2014 at 5:9 Comment(4)
How should ShellView looks like?Peen
Actually I have the TabControl ready with me, you can implement the same for TreeView, and actually this is more suitable for TreeViewOrgulous
And what if I want to activate one VM at the top of the View, the second at the bottom, and the last one in the center of a screen?Peen
you have to pass only those three in "ActivateItem" methodOrgulous

© 2022 - 2024 — McMap. All rights reserved.