使用MultiBinding对WPF数据网格文本列进行排序

时间:2013-06-01 13:18:53

标签: wpf wpfdatagrid

下面是来自我的WPF数据网格的DataGridTextColumn定义,其中包含转换器difing和转换器代码。有人可以请我如何排序这个专栏,我相信我需要使用Icomparer,但在尝试调整在线示例后,我的尝试失败了。如果你能在VB.Net中给出一个真正有用的解释 - 谢谢。

<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

1 个答案:

答案 0 :(得分:0)

由于您的转换器仅在TimeSpanEndDate之间计算StartDate,因此,如果您在对象中创建另一个公共TimeSpan属性,则会更容易我们称之为Duration,它将返回EndDate - StartDate并将其直接绑定到IMultiValueConverter,而不是DataGridTextColumn

<DataGridTextColumn Header="Duration" Width="70" x:Name="Duration" Binding="{Binding Path=Duration}">

对于演示文稿,我们会将其转换为ToString(),但仍会将其与TimeSpan进行比较,您可以像任何其他列一样对其进行排序