views:

17

answers:

1

I have a collection of Company objects contained in a CompaniesCollection which is inherited from Observable Collection. Via a CollectionViewSource, I am displaying these Companies in a list box. One of my requirements is that each company item must show the City and State of the first Address (of a collection of Addresses) attached to each Company.

I have tried everything I can think of to display those values, and I cannot seem to do it. I have verified that each Company object does indeed have at least one Address (most have two or more).

Here is the ControlTemplate for the ListBoxItemContainer I am using to display the Company:

<ControlTemplate TargetType="{x:Type ListBoxItem}">
    <Grid Height="Auto" Width="Auto" DataContext="{Binding Source={StaticResource CompanyDataSource}}">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
        <Border x:Name="Bd" BorderThickness="{TemplateBinding BorderThickness}" Background="#59000000" SnapsToDevicePixels="true" CornerRadius="10" Padding="3,1" d:LayoutOverrides="Width, Height" Grid.RowSpan="1">
            <Grid Height="Auto" Width="Auto" Margin="3,1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto" MinWidth="70"/>
                    <ColumnDefinition Width="*" MinWidth="30"/>
                    <ColumnDefinition Width="20" MinWidth="30"/>
                </Grid.ColumnDefinitions>
                <TextBlock TextWrapping="Wrap" Text="{Binding CompanyName}" Style="{DynamicResource TitleText}" Margin="0" VerticalAlignment="Top" d:LayoutOverrides="Width" Grid.ColumnSpan="2"/>
                <Grid Margin="0" Grid.Column="2" HorizontalAlignment="Right" VerticalAlignment="Top">
                    <Ellipse Fill="#FFAB1D1D" HorizontalAlignment="Left" Height="13" Stroke="#FFDE5B5B" VerticalAlignment="Top" Width="13"/>
                </Grid>
                <Grid Grid.Column="1" Grid.Row="1">
                    <Grid Grid.Column="0" Grid.Row="0">
                        <TextBlock Margin="0" TextWrapping="Wrap" Text="{Binding Addresses[0].City}" d:LayoutOverrides="Width, Height" Grid.Column="0" Grid.Row="0" Style="{DynamicResource SubtitleText}"/>
                    </Grid>
                </Grid>
                <TextBlock Margin="0" TextWrapping="Wrap" Text="{Binding CompanyName2}" d:LayoutOverrides="Width, Height" Grid.Row="1" Style="{DynamicResource SubtitleText}"/>
            </Grid>
        </Border>
        <Border BorderThickness="0" Grid.ColumnSpan="1" Grid.RowSpan="1" Margin="7,0" Grid.Row="1" CornerRadius="0,0,10,10" Background="#4CFFFFFF">
            <ListBox Background="#00000000" ItemTemplate="{DynamicResource ContactTemplate}" ItemsSource="{Binding Contacts}"/>
        </Border>
    </Grid>
</ControlTemplate>

I have also tried loading the Addresses (and Contacts for that matter) into CollectionViewSources in the ControlTemplate.Resources in an effort to try and access them to no avail. How does one access a child collection (and thereby a specific item in that collection) in a DataBound ListBoxItem? Any help would be immensely helpful.

+1  A: 

That depends on what your object's property that holds the collection of addresses looks like. But since it would be arguably easier, why not simply add a property to your Company named FirstAddress and return the first element in the collection in the getter? Then you can bind to FirstAddress.City etc. You're free to return null when the collection is empty or anything else for that matter and so you don't depend only on the bindings.

Alex Paven
I had through about doing that. There is no telling whether the future will bring further requirements like this having me add more and more items to the Company class to bypass the above behaviour. It will be an easy enough fix. Thanks for the input.
OffApps Cory