My requirement is to tell the user when the one value in a data grid row is less than another value. I'm going to tell them by making the first cell's foreground red and adding a tooltip. So I will add a trigger that will use a multi value converter.
So step one is to write a multi value converter that will take two values which are passed by binding and an operator which is passed as the parameter and return true or false. It's a nice generic converter so I will be able to use it in many places.
Public Class CompareConverter
Implements System.Windows.Data.IMultiValueConverter
' values(0) is decimal
' values(1) is decimal
' returns boolean
Public Function Convert(values() As Object, targetType As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object Implements System.Windows.Data.IMultiValueConverter.Convert
Dim LH As Decimal = 0
Dim Op As String = "="
Dim RH As Decimal = 0
Dim Result As Boolean = False
If values.Count > 0 Then Decimal.TryParse(values(0).ToString(), LH)
Op = parameter.ToString()
If values.Count > 1 Then Decimal.TryParse(values(1).ToString(), RH)
Select Case Op
Case "<" : Result = (LH < RH)
Case ">" : Result = (LH > RH)
Case "<>", "!=" : Result = (LH <> RH)
Case "<=" : Result = (LH <= RH)
Case ">=" : Result = (LH >= RH)
Case Else : Result = (LH = RH)
End Select
Return Result
End Function
Public Function ConvertBack(value As Object, targetTypes() As System.Type, parameter As Object, culture As System.Globalization.CultureInfo) As Object() Implements System.Windows.Data.IMultiValueConverter.ConvertBack
Throw New System.NotImplementedException
End Function
End Class
Of course we will need to create a static resource so we can reference it.
<local:CompareConverter x:Key="CompareConverter"/>
Now we need to add a style to the column to define a trigger.
<DataGridTextColumn Header="Total Account Chgs" Binding="{Binding Path=Debit, StringFormat='{}{0:0.00}'}">
<DataGridTextColumn.ElementStyle>
<Style TargetType="TextBlock">
<Setter Property="Foreground" Value="Black"></Setter>
<Setter Property="TextBlock.ToolTip" Value=""></Setter>
<Style.Triggers>
<DataTrigger Value="True">
<DataTrigger.Binding>
<MultiBinding Converter="{StaticResource CompareConverter}" ConverterParameter="<">
<Binding Path="Debit"></Binding>
<Binding Path="TotalPaid"></Binding>
</MultiBinding>
</DataTrigger.Binding>
<DataTrigger.Setters>
<Setter Property="Foreground" Value="Red"/>
<Setter Property="TextBlock.ToolTip" Value="The Total Account Charges are less than the Total Paid."/>
</DataTrigger.Setters>
<Setter Property="TextBlock.ToolTip" Value="The Total Account Charges are less than the Total Paid."/>
</DataTrigger.Setters>
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
No comments:
Post a Comment