0
votes

I have the following Xaml (Silverlight, but it shouldn't matter):

<ListBox x:Name="Results"> ... </ListBox>
<StackPanel DataContext="{Binding ElementName=Results, Path='SelectedItem.Attributes'}">
  <TextBlock Text="{Binding ElementName=Results, Path='SelectedItem.Attributes[ID]'}" />
  <TextBlock Text="{Binding '[ID]'}" />
</StackPanel>

When I populate the ListBox, the second TextBlock is populated, but the first TextBlock is not. When I select any Item from the Listbox the first TextBlock is populated, but the second doesn't change.

I'm assuming that I'm missing something to tell the StackPanel's DataContext that it needs to refresh any time I change the SelectedItem in my ListBox, but I'm at a loss on what I need to do.

Ideally, I'd like to not have to bind to the whole path for each of my TextBlocks (there are going to be a bunch of them).

1
Hi Rob,I think it looks good. I created a little example myself, and it worked (if you want, I'll post it). Could you provide some more information. What is ItemsSource of the list box, ... Maybe this helps to solve your problem. - TerenceJackson
I'm binding the ListBox in code behind to a List<obj> ... obj has a Dictionary<things> property called Attributes. I have it working when I bind stuff like I do in the top TextBlock, but I wanted to bind things like the second TextBlock. The bottom TextBlock gets bound, but only when I populate the ListBox, not when the selected item changed. The top TextBlock only gets populated then the ListBox's SelectedItem is changed, not when it's populated... - Rob

1 Answers

0
votes

Gaah... I found it. Some nitwit (you read.. me) decided that it'd be smart to override the stackpanel's datacontext when I populated the ListBox. Sorry for anyone that was working on this.