Search code examples
c#wpfvb.netimultivalueconverter

wpf update binding property on converter property change


I have a TextBlock which has a Foreground property bound with MultiBinding as following:

<TextBlock.Foreground>
    <MultiBinding Converter="{StaticResource BlToBrshConv1}">
        <Binding Path="SomePropertyOfOwnerClass" />
        <Binding Path="AnotherProperty"/>
    </MultiBinding>
</TextBlock.Foreground>

The converter BlToBrshConv1 is as the following:

Class BlToBrshConv1
 Implements IMultiValueConverter  
Property InheritedBrush as Brush
Public Function Convert(values() As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IMultiValueConverter.Convert
    Try
        Dim b1 As Boolean = CBool(values(0))     
        Dim b2 As Boolean = CBool(values(1))     
        If b1 = True AndAlso b2 = True Then
            ' Return SomeBrush0
        ElseIf b1 Then     
            '  Return SomeBrush1
        Else
            Return InheritedBrush
        End If
    Catch ex As Exception
        Return InheritedBrush
    End Try

Now my problem is when The property 'InheritedBrush' of the converter itself, I need to update the 'Foreground' brush.

The Foreground brush doesn't update because the bound properties ("SomePropertyOfOwnerClass" and "AnotherProperty") didnot change.

Any Ideas?


Solution

  • You could change the binding using triggers.

    <TextBlock>
        <TextBlock.Style>
            <Style TargetType="TextBlock">
                <Setter Property="Foreground" Value="{Binding Defaultbrush}"/>
                <Style.Triggers>
                    <DataTrigger Binding="{Binding SomePropertyOfOwnerClass}">
                        <Setter Property="Foreground" Value="{Binding SomeBrush1}"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding AnotherProperty}">
                        <Setter Property="Foreground" Value="{Binding InheritedBrush}"/>
                    </DataTrigger>
                    <MultiDataTrigger>
                        <MultiDataTrigger.Conditions>
                            <Condition Binding="{Binding SomePropertyOfOwnerClass}" Value="True"/>
                            <Condition Binding="{Binding AnotherProperty}" Value="True"/>
                        </MultiDataTrigger.Conditions>
                        <Setter Property="Foreground" Value="{Binding SomeBrush0}"/>
                    </MultiDataTrigger>
                </Style.Triggers>
            </Style>
        </TextBlock.Style>
    </TextBlock>