0

If I have a ListBox and it's bound to MyObject.MyCollection, how can I also get MyObject.MyValue?

<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;}
}
bwoogie
  • 4,339
  • 12
  • 39
  • 72

1 Answers1

0

I think you can use Relative source in the binding

<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>
Clemens
  • 123,504
  • 12
  • 155
  • 268
Justin CI
  • 2,693
  • 1
  • 16
  • 34