2017-09-14 74 views
0

如果我有一個ListBox並且它綁定到MyObject.MyCollection,我怎樣才能得到MyObject.MyValueListBox集合外的綁定屬性

<ListBox ItemsSource="{Binding Path=MyObject.MyCollection}"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel> 
     <TextBlock Text="{Binding Path=Name}"/> 
     <TextBlock Text="{Binding Path=MyObject.MyValue}"/> 
     </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

public class MyObject { 
    public ObservableCollection<CollectionThing> MyCollection {get; set;} 
    public string MyValue {get; set;} 
} 

回答

0

我認爲你可以在綁定

<ListBox ItemsSource="{Binding Path=MyObject.MyCollection}"> 
    <ListBox.ItemTemplate> 
     <DataTemplate> 
      <StackPanel> 
       <TextBlock Text="{Binding Path=Name}"/> 
       <TextBlock Text="{Binding Path=DataContext.MyObject.MyValue, 
            RelativeSource={RelativeSource AncestorType=ListBox}}"/> 
      </StackPanel> 
     </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 
使用相對源
相關問題