我想使用日期列对wpf中的列表视图进行排序。但下面的代码是排序字符串而不是日期。我已将数据表绑定到列表视图。这是我的密码。
<ListView.View>
<GridView x:Name="gvDetails" >
<GridViewColumn Header="Header1" Width="130" DisplayMemberBinding="{Binding Column1}" />
<GridViewColumn Header="Header2" Width="120" DisplayMemberBinding="{Binding Column2}" />
<GridViewColumn Header="Header3" Width="120" DisplayMemberBinding="{Binding Column3}" />
<GridViewColumn Header="DateOfEnquiry" Width="120" DisplayMemberBinding="{Binding DateOfEnquiry}" />
</GridView>
</ListView.View>
分拣代码:
ICollectionView dataView =
CollectionViewSource.GetDefaultView(lstvwDetails.ItemsSource);
if (dataView != null)
{
dataView.SortDescriptions.Clear();
SortDescription sd = new SortDescription("DateOfEnquiry", direction);
dataView.SortDescriptions.Add(sd);
}
但这不是按日期对listview日期列进行排序,而是仅按字符串进行排序。请告知。
我认为这个示例将对您有所帮助:
string propertyName = "DateOfEnquiry";
ICollectionView dataView = CollectionViewSource.GetDefaultView(lstvwDetails.ItemsSource);
ListSortDirection direction = ListSortDirection.Ascending;
if (dataView.SortDescriptions.Count > 0 && dataView.SortDescriptions[0].PropertyName == propertyName)
{
if (dataView.SortDescriptions[0].Direction == ListSortDirection.Ascending) direction = ListSortDirection.Descending;
else direction = ListSortDirection.Ascending;
}
dataView.SortDescriptions.Clear();
dataView.SortDescriptions.Add(new SortDescription(propertyName, direction));