wpf在转换器属性更改时更新绑定属性

时间:2018-11-12 20:05:39

标签: c# wpf vb.net imultivalueconverter

我有一个TextBlock,它具有与MultiBinding绑定的Foreground属性,如下所示:

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

转换器BlToBrshConv1如下:

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

现在我的问题是,当转换器本身的属性'InheritedBrush'时,我需要更新'Foreground'画笔。

前景笔刷不会更新,因为绑定属性(“ SomePropertyOfOwnerClass”和“ AnotherProperty”)没有更改。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以使用触发器更改绑定。

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