views:

100

answers:

2

I'm trying to databind to a listbox like so:

<ListBox x:Name="MyListBox" Margin="0,0,0,65">
 <ListBox.ItemTemplate>
   <DataTemplate>
      <TextBlock Text="{Binding Converter={StaticResource MyConverter}}" /> 
   </DataTemplate>
 </ListBox.ItemTemplate>
</ListBox>

The reason I am binding to the whole object and not a property is because my converter will need multiple properties of the object to build the string that returns.

This works and my string is returned. But then when I change the ObservableCollection that this is based on the value doesn't change on the screen. If I bind to just a single property and change it, then the value does change.

Any idea what I can do differently? I can't bind to a single property since I need the entire object in the converter... And the ConverterParameter is already being used.

+3  A: 

Remember, if you bind to the "main" property and the value of the main property itself isn't changed, the binding will have no reason to refresh itself. It has no clue that your converter is actually based off of a sub-property. What you can do is use a MultiBinding where you bind not only the "main" property, but also a specific sub-property. This gives your IMultiValueConverter implementation access to the main data object, but because you're also binding to the sub-property that's changing, will also be refreshed when that sub-property's value changes.

Drew Marsh
Ooh, nice idea.. Never thought of that one. I'll give it a go.
Kelly
Works perfect! Thanks, I've been beating my head over this one for the better part of the day..
Kelly
Glad I could help. ;)
Drew Marsh
+1  A: 

You can try using a MultiBinding which I believe updates whenever any of its Bindings are triggered. You can also use an IMultiValueConverter or just take advantage of the StringFormat of the binding.

Will