Binding to property of specific collection item in a ListBoxItemContainerStyle

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.

Answers


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.


Need Your Help

Hide Html inside of a <td> not the <td> itself

javascript jquery html css

This has to be a crazy basic question, but for the life of me I can't figure it out right now...

Update UIView after Locking and Unlocking phone

ios objective-c multithreading uiview

My app is running an NSURLSession where it downloads a file. However, when the user locks the phone I cannot update the subviews on the screen. They are frozen. I have a method getting called when ...