R – WPF databinding and converters

converterdata-bindingwpf

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 it 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.

What can I 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.

Best Answer

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.