0
votes

I have a ListView bound to an ObservableCollection of CustomerContacts. It works great so far, but being new to this, I'm not sure how to do the next part. Each customer contact has several contact types, which I want to display under their name.

So inside of CustomerContacts I have another ObservableCollection of ContactTypes.

Here is my current datatemplate:

    <DataTemplate x:Key="iconTemplate">
        <DockPanel Height="133" Width="150">
            <Image Source="/Tracks%203.5;component/Images/NoPic.png" Height="25" Width="25" Margin="1,0" />
            <TextBlock DockPanel.Dock="Top" Text="{Binding FullName}" Margin="5,3,5,0" FontWeight="Bold" HorizontalAlignment="Left" />
            <<TextBlock Text="{Binding Title}" Margin="5,0,5,3" HorizontalAlignment="Left" />>
        </DockPanel>
    </DataTemplate>

And here's my first attempt at putting the listview inside:

    <DataTemplate x:Key="iconTemplate">
        <DockPanel Height="133" Width="150">
            <Image Source="/Tracks%203.5;component/Images/NoPic.png" Height="25" Width="25" Margin="1,0" />
            <TextBlock DockPanel.Dock="Top" Text="{Binding FullName}" Margin="5,3,5,0" FontWeight="Bold" HorizontalAlignment="Left" />
            <ListView ItemsSource="{Binding ContactTypes}">
                <ListView.Template>
                    <ControlTemplate TargetType="ItemsControl">
                        <ItemsPresenter/>
                    </ControlTemplate>
                </ListView.Template>
                <ListView.ItemsPanel>
                    <ItemsPanelTemplate>
                        <StackPanel Orientation="Horizontal"/>
                    </ItemsPanelTemplate>
                </ListView.ItemsPanel>
                <ListView.ItemTemplate>
                    <DataTemplate>
                        <TextBlock Margin="3,0,0,0" HorizontalAlignment="Center" Text="{Binding Path=ContactType}"/>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
            <!--<TextBlock Text="{Binding Title}" Margin="5,0,5,3" HorizontalAlignment="Left" />-->
        </DockPanel>
    </DataTemplate>

I want to replace the TextBlock bound to Title with a ListView/ListBox/ItemsControl bound to the items in ContactTypes.

Somewhat similar to this question: < WPF: bind to a List inside a class > but without all the codebehind. It would be nice to have it in the XAML.

1
Have you tried adding the listView? What happened when you tried?Phil Sandler
I think it isn't working because my inner ListView <ListView ItemsSource="{Binding ContactTypes}"> isn't actually binding to the ObservableCollection. For instance, in my outer Listview, I have to set the ItemsSource in the codebehind Me.lstContacts.ItemsSource = contacts otherwise it wont work. I think I have to figure out how to make the ObservableCollection contacts into a windows property, and maybe I have to do that with the inner listview too? MakeContactTypes into a property?AndyD273

1 Answers

1
votes

There's a couple things I would do here:

  1. Simplify what you're trying to do in order to isolate the problem. Instead of that whole ListView business, just put in there an ItemsControl: <ItemsControl ItemsSource="{Binding ContactTypes}" /> That'll give you the default view of things, which may just show you the data type of your objects (whatever ContactTypes is), but it'll let you know if the binding is working. If it is, you'll get something listed there. If it isn't, you won't.

  2. If you're not getting anything listed, go use Snoop to drill into it and look for errors. Snoop shows the databinding errors, allows you to inspect the DataContext of each of the items, etc.

  3. If you're still having problems, it might benefit us if you posted your class definitions and your code where you wire things up. There might be some other underlying issues (for instance, is the ContactTypes property null when the binding initially occurs and you're not using INPC to let the binding system know when it changes?).