2015-01-16 106 views
0

我有一個從ListBoxItemtemplate綁定的問題,它包含一個Expander以及它自己的HeaderTemplateContentTemplate來自列表框的WPF綁定ItemTemplate

<ListBox ItemsSource="{Binding Feeds}"> 
     <ListBox.ItemTemplate> 
      <DataTemplate> 
       <Expander Content="{Binding Text}" 
          Header="{Binding Title}"> 

        <Expander.HeaderTemplate> 
         <DataTemplate> 
          <DockPanel > 
           <TextBlock Text="{Binding PublishDate}" /> 
           <TextBlock Text="{Binding}" /> 
          </DockPanel> 
         </DataTemplate> 
        </Expander.HeaderTemplate> 

        <Expander.ContentTemplate> 
         <DataTemplate> 
          <StackPanel Orientation="Vertical"> 
           <Image Source="{Binding ImageURL}" /> 
           <TextBlock Text="{Binding}" /> 
          </StackPanel> 
         </DataTemplate> 
        </Expander.ContentTemplate> 
       </Expander> 
      </DataTemplate> 
     </ListBox.ItemTemplate> 
    </ListBox> 

的PublishDate和IMAGEURL綁定不工作,我想將它們綁定到屬性的對象飼料:

Public Class Feed 
    Public Property Title As String 
    Public Property PublishDate As DateTime 
    Public Property Text As String 
    Public Property ImageURL As String 
End Class 

我在結合試圖FindAncestor找到ListBoxItem但它沒有幫助我。

回答

1

擴展器的內容充當ContentTemplate的DataContext和HeaderTemplate的Header。代替

Set Content =「{Binding}」和Header =「{Binding}」。這將兩個模板的DataContext設置爲ListBoxItem的DataContext(即Feed)。