tags:

views:

93

answers:

1

My ItemContainerStyle works perfectly when a ListViewItem is added:

   <Style x:Key="ItemContStyle"
           TargetType="{x:Type ListViewItem}">
        <Style.Resources>
            <SolidColorBrush x:Key="lossBrush"
                             Color="Red" />
            <SolidColorBrush x:Key="newPartNo"
                             Color="LightGreen" />
            <SolidColorBrush x:Key="noSupplier"
                             Color="Yellow" />
            <Orders:OrderItemStatusConverter x:Key="OrderItemConverter" />
        </Style.Resources>
        <Style.Triggers>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=DataContext, Converter={StaticResource OrderItemConverter}}"
                         Value="-1">
                <Setter Property="Background"
                        Value="{StaticResource lossBrush}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=DataContext, Converter={StaticResource OrderItemConverter}}"
                         Value="-2">
                <Setter Property="Background"
                        Value="{StaticResource newPartNo}" />
            </DataTrigger>
            <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self},Path=DataContext, Converter={StaticResource OrderItemConverter}}"
                         Value="-3">
                <Setter Property="Background"
                        Value="{StaticResource noSupplier}" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

However when the source item changes, the trigger is not fired and the background colour is not what I expect.

How can I make the trigger fire?

+1  A: 

Perhaps the Path on the bindings should be to a property held by the DataContext rather than the DataContext itself. That property would return the -1, -2, -3 or whatever. When that value changed, the triggers would be evaluated. The problem here is once loaded, the DataContext most likely isn't changing.

Scott J
I think this is the problem, also, and it looks like the converter is doing some work to produce this value.Also, just to clarify the xaml, all the bindings can be simplified to: {Binding Converter={StaticResource OrderItemConverter}}, as Self.DataContext is the default source for a binding. If the work done in the converter were moved to the ViewModel, it could just be {Binding}.
Abe Heidebrecht
I've made it work using this approach - thanks. Still not ideal though.
hayrob