2011-08-28 36 views
2

我用MVVM模式綁定嵌套控件存在問題。這是我的XAML代碼:使用MVVM模式綁定嵌套控件

<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> 

問題是,位於ExpanderControl數據模板中的列表框控件沒有被數據綁定。 ListBox控件由EntityCollection填充名爲'Messages',它包含在ItemsControl與數據綁定的父對象'NotificationContacts'中...

有誰知道如何解決這個問題?

在此先感謝!

+0

那麼你的視圖模型是什麼樣子? – aL3891

回答

0

Call ItemsControl f.i. 「IC」和未來使用ListBox中結合

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

感謝您的幫助。不幸的是,你的解決方案並不適合我....列表框仍然沒有被數據填充... – Czarek

+0

哪個類包含Messages屬性? –

1

你試試這個:

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

如果我記得是正確的,當你的「內部」 ItemContol,結合上下文設置爲NotificationContacts。所以只需使用「{綁定消息}」就可以了。

順便說一下,你就行缺少大括號:

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