Czarek
Czarek

Reputation: 21

Binding nested control using MVVM pattern

I have a problem with binding nested control with my MVVM pattern. This is my XAML code:

 <ItemsControl Grid.Column="1" ItemsSource="{Binding NotificationContacts}">
            <ItemsControl.ItemTemplate>
                <DataTemplate>
                    <toolkit:Expander>
                        <toolkit:Expander.Header>
                            <Grid>
                                <Grid.ColumnDefinitions>
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                    <ColumnDefinition Width="Auto" />
                                </Grid.ColumnDefinitions>
                                <TextBlock Text="{Binding ContactName}" Grid.Column="0" VerticalAlignment="Center"></TextBlock>
                                <Image Source="Images/answer_ok.png" Grid.Column="1" Margin="15,0,15,0" Width="27" Height="27"></Image>
                            </Grid>
                        </toolkit:Expander.Header>
                        <toolkit:Expander.Content>
                            <ListBox Margin="30,10,0,10" ItemsSource="{Binding NotificationContacts.Messages">
                                <ListBox.ItemTemplate>
                                    <DataTemplate>
                                        <TextBlock Text="{Binding MessageName}"></TextBlock>
                                    </DataTemplate>
                                </ListBox.ItemTemplate>
                            </ListBox>
                        </toolkit:Expander.Content>
                    </toolkit:Expander>
                </DataTemplate>
            </ItemsControl.ItemTemplate>
        </ItemsControl>

The problem is, that the listbox control located in ExpanderControl Data Template is not being data bound. Listbox control is populated by EntityCollection named 'Messages' which is contained in parent object 'NotificationContacts' that ItemsControl is databound with...

Does anyone know how to resolve this issue ?

Thanks in advance !!!

Upvotes: 2

Views: 673

Answers (2)

Lukas Pirkl
Lukas Pirkl

Reputation: 1447

Did you try this:

<ItemsControl Grid.Column="1" ItemsSource="{Binding NotificationContacts}">
  ......
   <ListBox Margin="30,10,0,10" ItemsSource="{Binding Messages}">
  .....
       <TextBlock Text="{Binding MessageName}"></TextBlock>

If I remember it right, when you are "inside" ItemContol, binding context is set to NotificationContacts. So use just "{Binding Messages}" could be fine.

And by the way, you are missing curly bracket on the line:

<ListBox Margin="30,10,0,10" ItemsSource="{Binding NotificationContacts.Messages">

Upvotes: 1

Vladimir Dorokhov
Vladimir Dorokhov

Reputation: 3839

Call ItemsControl f.i. "ic" and use next binding in ListBox

<ItemsControl x:Name="ic" Grid.Column="1" ItemsSource="{Binding NotificationContacts}">
     ...
     <ListBox Margin="30,10,0,10" ItemsSource="{Binding ElementName=ic, Path=DataContext.Messages}">

Upvotes: 0

Related Questions