I've got a ListBox, containing Messages, which are simple (typeEnum, string) objects, and are displayed using a datatemplate:
<ListBox ItemsSource="{Binding Messages}"
Background="DimGray" >
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal" >
<TextBlock Text="{Binding MessageText}" >
<TextBlock.Foreground>
<MultiBinding Converter="{StaticResource TypeEnumToBrushConverter}" >
<Binding Path="TypeEnum" />
<Binding Path="MessageColours" RelativeSource="{RelativeSource AncestorType={x:Type Window}}" />
</MultiBinding>
</TextBlock.Foreground>
</TextBlock>
<Button Click="ButtonBase_OnClick1" >T</Button>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
I'm trying to set the foreground of the message to whatever the MessageColours (a collection of TypeEnum - Brush pairs), which I pass into the converter.
This works for the initial brushes I manually add to MessageColours.
I'm changing them in another window, and the collection gets updated successfully, but the ListBox message colour does not update. MessageColours was originally a ObservableCollection of MessageColourSettings (TypeEnum, Brush) but I changed it to an implementation that implements INotifyPropertyChanged so it notifies when properties in a collection item change as well, but it didn't help.
MessageColourSettings implements INotifyPropertyChanged and it is firing when the color is changed on the other window, and MessageColours collection is firing its item property changed.
When I click that button, it toggles the typeEnum from A to B and back to A, upon which the message now has the updated colour, so I think something is wrong with the multibinding.
The converter:
public class TypeEnumToBrushConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
TypeEnum typeEnum = (TypeEnum) values[0];
var settings = values[1] as FullyObservableCollection<MessageColourSetting>;
return settings.Single(setting => setting.TypeEnum == typeEnum).Brush;
}
}
Any insights would be appreciated