Quantcast
Channel: patterns & practices: Prism
Viewing all articles
Browse latest Browse all 1878

New Post: Possible to register the same ViewModel with Different names?

$
0
0
Hi,

Can you create multiple NonShared ViewModels with a different name? My problems is I have a composite view:
<UserControl x:Class="Natsar.ODS.Documents.Spoken.SpokenView"
.
..
...
<Grid x:Name="LayoutContent" HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
    <Border Padding="5,5,5,5" BorderBrush="#77000000" BorderThickness="1,1,1,1" Margin="0,0,0,5" CornerRadius="12,12,12,12" Width="Auto">
        <ContentControl x:Name="FlowDocumentDetails" Content="{Binding FlowDocumentDetails}">
            <ContentControl.ContentTemplate>
                <DataTemplate>
                    <flowdoc:FlowDocumentView/>
                </DataTemplate>
            </ContentControl.ContentTemplate>
        </ContentControl>
    </Border>
</Grid>
</UserControl>
..the SpokenView gets created and added manually when the user clicks a Button on the Custom TabControl. The Button click event is added to the TabControl during initialization of the template:
public override void OnApplyTemplate()
{
    base.OnApplyTemplate();

    .
    ..
    ...
    
    // set up the event handler for the 'New Tab' Button Click event
    _addNewButton = this.Template.FindName("PART_NewTabButton", this) as ButtonBase;
    if (_addNewButton != null)
        _addNewButton.Command = this.VerseTabCommand;
}
..and here is when the SpokenView is manually created:
public void AddTabItem(SelectedReferenceState selectedReferenceState, /*SpokenTabType spokenTabType, */string parameter)
{
    .
    ..
    ...
    
        SpokenTabItem tabItem;

        // Using Items Property
        ISpokenViewModel spokenViewModel = ServiceLocator.Current.GetInstance<ISpokenViewModel>();
        SpokenView view = new SpokenView(selectedReferenceState);
        view.ViewModel = spokenViewModel;
        spokenViewModel.SelectedReferenceState = selectedReferenceState;
        tabItem = new SpokenTabItem { Header = parameter, Content = view, SpokenTabKey = parameter };

        int v = 0;
        if (selectedReferenceState.spokenTabType == SpokenTabType.Verse)
            v = this.Items.Add(tabItem);
        else
            if (selectedReferenceState.spokenTabType == SpokenTabType.Chapter)
                this.Items.Insert(0, tabItem);
            else
                if (i == -1 || i == this.Items.Count - 1 || AddNewTabToEnd)
                    v = this.Items.Add(tabItem);
                else
                {
                    v = i;
                    this.Items.Insert(++i, tabItem);
                }
    ...
    ..
    .
}
..but, I'm unable to get the FlowDocumentViewModel of the FlowDocumentView from the SpokenViewModel when its being automatically created when the SpokenView has completed its initialization.

I hope I can get you to understand this. Each TabItem needs its own NonShared objects of SpokenView and FlowDocumentViewModel, but at the same time SpokenView needs access to the FlowDocumentViewModel.

So is there a way I can create an object instance with a unique name like Unity. Is this available in MEF OR I can accomplish this? This looked interesting C# MEF: Exporting multiple objects of one type, and Importing specific ones, but still trying to visually construct this to work for my situation.

Viewing all articles
Browse latest Browse all 1878

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>