1

Closed

One Viewmodel reference injected into multiple disappears

description

I'm injecting one viewmodel into multiple Viewmodels that are represented as a collection.

So let's say I have a TabControl and each Tab has the same instance of another ViewModel that I display there (this could be a informationBar about something or whatever).

Now It displays only in the most recent tab. So When I look at Tab1 it's there, if I then go to Tab2 it appears there but now never again in Tab1.

Here is the code to reproduce this problem:
public class ShellViewModel : Conductor<TabViewModel>.Collection.OneActive, IShell
{
    public ShellViewModel()
    {
        SameInEveryTabViewModel same = new SameInEveryTabViewModel();

        ActivateItem(new TabViewModel("Tab1", same));
        Items.Add(new TabViewModel("Tab2", same));
        Items.Add(new TabViewModel("Tab3", same));
    }
}
<Window x:Class="CMTest.ShellView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">

<TabControl x:Name="Items" />
</Window>
public class TabViewModel : Screen
{

    private SameInEveryTabViewModel _same;
    public SameInEveryTabViewModel Same
    {
        get { return _same; }
        set
        {
            _same = value;
            NotifyOfPropertyChange("Same");
        }
    }

    public TabViewModel(string displayName, SameInEveryTabViewModel same)
    {
        DisplayName = displayName;
        this.Same = same;
    }
}


<UserControl x:Class="CMTest.TabView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="auto" />
        <RowDefinition Height="*" />
    </Grid.RowDefinitions>

    <ContentControl x:Name="Same" />

    <TextBlock Grid.Row="1" x:Name="DisplayName" FontSize="30" />

</Grid>
</UserControl>
public class SameInEveryTabViewModel : Screen
{
    public SameInEveryTabViewModel()
    {
        DisplayName = "Same";
    }
}


<UserControl x:Class="CMTest.SameInEveryTabView"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Grid>
    <TextBlock x:Name="DisplayName" />
</Grid> 
</UserControl>


If I would skip with defining SameInEveryTabViewModel in a seperate view and just do it right there in the TabView then it would work fine with a normal old style binding. But of course you would wan't to be able to define it seperatly because SameInEveryTabViewModel might not always be the same.

file attachments

Closed Aug 28, 2012 at 1:13 PM by EisenbergEffect
Not an issue.

comments

BladeWise wrote Jul 18, 2012 at 2:29 PM

If the view model implements IViewAware, the view is cached, so the same view is associated to multiple controls, which is not possibile in WPF/Silverlight (the same control has a single visual or logical parent).
Either disable view-caching, or avoid inheriting from Screen.

tibel wrote Jul 21, 2012 at 11:40 AM

This is not an issue, but should be described in the documentation.