Issues with view location for collection of view models

Topics: Conventions, UI Architecture
Oct 16, 2011 at 8:47 PM
Edited Oct 16, 2011 at 9:27 PM

PLEASE IGNORE, I found out the root cause which had to do with the addin layout of my application.

Hi, I am experiencing an odd behavior that I can't understand, as it usually works just fine.

I have a view model which contains and displays some other view models. When I show them individually using a content control everything works as expected, but when I show them in an ItemsControl their view cannot be located.

This is the main view model, it simply exposes SomeViewModel instances both individually and in a collection.

 

public class TroublesomeViewModel : PropertyChangedBase
{
    private ObservableCollection<SomeViewModel> troublesomeItems;

    public TroublesomeViewModel()
    {
        TroublesomeItems = new ObservableCollection<SomeViewModel>(new[]
        {
            new SomeViewModel {SomeText = "hello"},
            new SomeViewModel {SomeText = "bye"}
        });
    }

    public ObservableCollection<SomeViewModel> TroublesomeItems
    {
        get { return troublesomeItems; }
        set
        {
            troublesomeItems = value;
            TroublesomeItem = value[0];
            AnotherTroublesomeItem = value[1];
            NotifyOfPropertyChange(() => TroublesomeItems);
        }
    }

    private SomeViewModel troublesomeItem;
    public SomeViewModel TroublesomeItem
    {
        get { return troublesomeItem; }
        set
        {
            troublesomeItem = value;
            NotifyOfPropertyChange(() => TroublesomeItem);
        }
    }

    private SomeViewModel anotherTroublesomeItem;
    public SomeViewModel AnotherTroublesomeItem
    {
        get { return anotherTroublesomeItem; }
        set
        {
            anotherTroublesomeItem = value;
            NotifyOfPropertyChange(() => AnotherTroublesomeItem);
        }
    }
}

 

Here is its view:

 

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
        <RowDefinition></RowDefinition>
    </Grid.RowDefinitions>
    <ItemsControl x:Name="TroublesomeItems" Grid.Row="0"></ItemsControl>
    <ContentControl x:Name="TroublesomeItem" Grid.Row="1"></ContentControl>
    <ContentControl x:Name="AnotherTroublesomeItem" Grid.Row="2"></ContentControl>
</Grid>

 

SomeViewModel has just one property and its view, called SomeView, displays it in a TextBlock. What I see when running the application is that the two individual VM are displayed correctly, while those in the ItemsControl cannot be displayed. That is, in the first row of the grid I get two "Cannot find view for SomeViewModel" while in the two ContentControls I get "hello" and "bye", respectively.

Any help is appreciated.

Oct 16, 2011 at 10:21 PM

By default, CM does not provides view resolution for Items inside an ItemsControl.

I created a WPF-only solution for this, whose source code is provided here. Note that the solution is limited to WPF due to the fact that it uses a DataTemplateSelector to provide view location (not supported in SL as far as I know... at least at the time of my post).

Oct 16, 2011 at 10:23 PM

Thanks for you reply, but CM does indeed provide that, I was doing something else wrong.

Oct 16, 2011 at 10:30 PM

Yep, you are right. I missed the update to the ConventionManager. :)