Thursday, May 19, 2016

DataTrigger with a MultiBinding

Framework 3.5

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="&lt;">
                            <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>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGridTextColumn.ElementStyle>
</DataGridTextColumn>


Tuesday, May 17, 2016

Simple approach to dealing with software criticism

A colleague of mine recently sent me a flow diagram for dealing with software criticism which I have reproduced below without attribution.



I thought it was a bit too complex so I simplified it.


I didn't say it was effective!

Friday, May 6, 2016

Political stupidity

For years, like millions of other developers, we have used the term "Dirty Page" to describe a common feature whereby the page knows if the user has changed something and prompts the user to save if they try to exit the page before saving. We also use the page's dirty state to enable the save and cancel buttons.


Yesterday we were all informed by senior management that the term "Dirty Page" has negative connotations and cannot be used in future. We were not given an alternative term to use. So far we have...

"The page formerly known as dirty"
"Stinky, filthy, disgusting page"
"If you think this is dirty you should see the code"
"Preserve My Stuff"

Your tax payer dollars at work!