DataTrigger正在第一次评估

时间:2012-05-04 12:25:54

标签: wpf

我有以下XAML代码,我试图根据转换器和DataTrigger设置Toolkit DataGrid中每行的前景。

转换器检查接收的对象,该对象包含其他对象的集合,实现IDataErrorInfo接口。如果集合中的项目有错误,则颜色设置为橙色,否则设置为蓝色。如果集合不包含非空项目,则颜色设置为黑色。

现在,当我从UI修改集合时,一切都很好,第一次正确设置颜色。但是,似乎DataTrigger不再被评估,因为转换器不会在调试模式下停止。

我无法理解我所缺少的东西。

我的XAML:

<tk:DataGrid.RowHeaderStyle >
<Style BasedOn="{StaticResource ResourceKey={x:Type tk:DataGridRowHeader}}" TargetType="tk:DataGridRowHeader">
    <Style.Triggers>
        <DataTrigger Binding="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type tk:DataGridRow}}, Path=DataContext.Payload.TimeEventFunctions[0].IsEmpty}" Value="False">
            <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Mode=FindAncestor,AncestorType={x:Type tk:DataGridRow}}, Path=DataContext.Payload, Converter={inf:DataGridRowHeaderForegroundConverter}}" />
            <!--<Setter Property="Foreground" Value="DodgerBlue" />-->
        </DataTrigger>
    </Style.Triggers>
</Style>

和转换器:

Public Class DataGridRowHeaderForegroundConverterExtension
Inherits Markup.MarkupExtension
Implements IValueConverter

Public Overrides Function ProvideValue(serviceProvider As System.IServiceProvider) As Object
    Return Me
End Function

Public Function Convert(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.Convert
    Dim ret As New SolidColorBrush(Colors.Black)
    If TypeOf value Is ISampleTableEntry Then
        Dim ste As ISampleTableEntry = DirectCast(value, ISampleTableEntry)
        Dim tevs As TrulyObservableCollection(Of ITimeEvFunc) = ste.TimeEventFunctions
        If tevs.Count > 0 AndAlso Not tevs(0).IsEmpty Then
            Dim query = From t In tevs Where t.HasErrors Select t
            If query IsNot Nothing Then
                Dim ErrorsPresent As Boolean = query.Count > 0
                ret = New SolidColorBrush(Color.FromArgb(255, 255, 200, 0))
            Else
                ret = New SolidColorBrush(Colors.DodgerBlue)
            End If
        End If
    End If
    Return ret
End Function

Public Function ConvertBack(value As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IValueConverter.ConvertBack
    Throw New NotSupportedException
End Function

End Class

感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

我认为值得检查视图模型中的“IsEmpty”属性设置器。您必须从“IsEmpty”属性设置器中提升属性更改事件..

并确保通过setter将默认值设置为“Foreground”。不要为标记设置前景的默认值。