Below is a DataGridTextColumn definition from my WPF datagrid with the converter difination and the converter code. Can someone please tech me how to sort this column, I believe I need to use Icomparer but after trying to adapt online example my attempts have failed. If you could give an explanation in VB.Net that would really help - thanks.
<Window.Resources>
<c:DurationConverter x:Key="MyDurationConverter"/>
<c:DistanceConverter x:Key="MyDistanceConverter"/>
</Window.Resources>
<DataGridTextColumn Header="Duration" Width="70" x:Name="Duration">
<DataGridTextColumn.Binding >
<MultiBinding Converter="{StaticResource MyDurationConverter}">
<Binding Path="StartDate" />
<Binding Path="EndDate"/>
<Binding Mode="OneWay" />
</MultiBinding>
</DataGridTextColumn.Binding>
</DataGridTextColumn>
Friend Class DurationConverter
Implements IMultiValueConverter
Friend 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
Return (CType(values(1), DateTime) - CType(values(0), DateTime)).ToString
End Function
Friend 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 NotImplementedException
End Function
End Class
Since your converter only calculates TimeSpan
between EndDate
and StartDate
, in your case, it will be much easier if you create another public TimeSpan
property in your object, lets call it Duration
, which will return EndDate - StartDate
and bind that directly, without IMultiValueConverter
, to your DataGridTextColumn
.
<DataGridTextColumn Header="Duration" Width="70" x:Name="Duration" Binding="{Binding Path=Duration}">
For presentation it will be converted ToString()
but will still compare it as TimeSpan
and you will be able to sort by it like any other column