Binding ItemsControl to a property of object

Topics: Conventions, Framework Services, UI Architecture
Oct 21, 2011 at 9:40 AM

Hi

I'm currently trying to learn WPF and CM and I've hit a bump. I'm making a small sample app that is very similar to the "Build your own MVVM framework" sample project.

What I'm trying to do is to bind an ItemsControl to a property of type object all it does is list out the items but it prints out the FQ Name of the type, "AssetTracker.ViewModels.IndividualResultViewModel". I.e it does a ToString on it.
However, when I change the property type of ObservableCollection<IndividualResultViewMode> it works fine. It bind up everything.

Any idea what I'm doing wrong?

Oct 23, 2011 at 7:21 PM
Edited Oct 23, 2011 at 7:22 PM

because the ItemsControl doesn't know how to represent the underlying object.  What does your XAML code look like to display the information?  Think datatemplate.

Oct 25, 2011 at 7:56 AM
This is the hosting control:
<UserControl x:Class="AssetTracker.Views.SearchResultsView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:Micro="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro">
    <StackPanel>
        <TextBlock Text="Search results" />
        <ItemsControl x:Name="Results" >
            
        </ItemsControl>
        
    </StackPanel>
</UserControl>
and this is one of the possible controls I want to display:
<UserControl x:Class="AssetTracker.Views.IndividualResultView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:Micro="clr-namespace:Caliburn.Micro;assembly=Caliburn.Micro" xmlns:local="clr-namespace:AssetTracker.UserControls">
    <local:HyperlinkButton x:Name="Open">
        <TextBlock x:Name="Name" Text="{Binding Name}" />
    </local:HyperlinkButton>
</UserControl>

I also want to display a NoResultsView as well, although I haven't created that one. Essentially it is the same thing as done in the GameLibrary example where I show the list of results if it contains results or show a message if no results where returned.
The backing field looks like this:

public object Results { get; set; }
I can create a small sample app that is usefull to you.
Oct 25, 2011 at 9:01 AM

Never mind. I found out what was wrong.

In the Results property I was setting it to either a NoResultsViewModel or ObservableCollection<IndividualResultViewModel>.  When I changed ObservableCollection<IndividualResultViewModel> to ResultViewModel which contains that collection it was able to resolve the views properly.