我有以下 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
谢谢你的帮助。