我正在尝试创建自定义排序WPF数据网格。我在每次排序时都进行过数据库调用。我已经使用了MVVM。
问题是当我进行数据库调用时,我总是在排序事件中获得列的排序方向始终为null。如果我从排序事件中删除UpdateData方法,它将正常工作。因此,默认情况下,它将根据我的代码按升序排序。任何人都可以帮助我获得排序方向或ASC和DESC吗?
<DataGrid HorizontalAlignment="Stretch" Grid.Row="1" Grid.Column="0" SelectedItem="{Binding SelectedPerson}" ItemsSource="{Binding CollectionView}" GridLinesVisibility="All" AutoGenerateColumns="False" SelectionUnit="FullRow" BorderThickness="1" IsReadOnly="True" >
<DataGrid.ContextMenu>
<ContextMenu>
<MenuItem Header="ADD" Command="{Binding AddNewPersonCommand}">
<MenuItem.Icon>
<Image Source="../Icons/Add.png" Height="16" Width="16" Margin="0" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="EDIT" Command="{Binding EditPersonCommand}">
<MenuItem.Icon>
<Image Source="../Icons/edit.png" Height="16" Width="16" Margin="0" />
</MenuItem.Icon>
</MenuItem>
<MenuItem Header="DELETE">
<MenuItem.Icon>
<Image Source="../Icons/delete.png" Height="16" Width="16" Margin="0" />
</MenuItem.Icon>
</MenuItem>
</ContextMenu>
</DataGrid.ContextMenu>
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseDoubleClick" >
<i:InvokeCommandAction Command="{Binding EditPersonCommand}" />
</i:EventTrigger>
<i:EventTrigger EventName="Sorting">
<command:EventToCommand Command="{Binding PersonGridSortingCommand}" PassEventArgsToCommand="True" />
</i:EventTrigger>
</i:Interaction.Triggers>
<DataGrid.Columns>
<DataGridTextColumn Header="Record ID" SortMemberPath="RecordID" Binding="{Binding RecordID}" Width="*" />
<DataGridTextColumn Header="Last Name" Binding= "{Binding LAST_NAME}" Width="*" />
<DataGridTextColumn Header="2nd Last Name" Binding= "{Binding SecondLastName}" Width="*" />
<DataGridTextColumn Header="First Name" Binding= "{Binding FIRST_NAME}" Width="*" />
<DataGridTextColumn Header="Date Of Birth" Binding= "{Binding Date_Of_Birth}" Width="*" />
<!--<DataGridTextColumn Header="Town" Binding= "{Binding PrimaryAddress.City}" Width="*" />-->
</DataGrid.Columns>
</DataGrid>
private ICollectionView _collectionView;
public ICollectionView CollectionView
{
get
{
return _collectionView;
}
set
{
_collectionView = value;
OnPropertyChanged("CollectionView");
}
}
public async Task UpdateData(string sortCoulmnName = null, string sortDirection = null)
{
var lstPersons = await PersonDAO.SelectAll(PagerViewModel.CurrentPage, PagerViewModel.PageSize, sortCoulmnName, sortDirection);
if (lstPersons.DataSource != null) { }
Persons = new ObservableCollection<PersonDTO>((PersonListDTO)lstPersons.DataSource);
CollectionView = CollectionViewSource.GetDefaultView(Persons);
}
public async void Sorting(DataGridSortingEventArgs e)
{
e.Handled = true;
ListSortDirection direction = (e.Column.SortDirection != ListSortDirection.Ascending) ? ListSortDirection.Ascending : ListSortDirection.Descending;
e.Column.SortDirection = direction;
string dir = direction.ToString() == "Ascending" ? "ASC" : "DESC";
await UpdateData(e.Column.SortMemberPath, dir);
CollectionView.SortDescriptions.Clear();
CollectionView.SortDescriptions.Add(new SortDescription(e.Column.SortMemberPath, direction));
}
答案 0 :(得分:0)
由于您将事件标记为已处理(e.Handled = true
),因此您实际上阻止了DataGrid的排序,并且SortDirection永远不会更改。
您必须自己管理排序方向。但它应该像这样简单:
private ListSortDirection lastSortDirection;
private string lastSortMemberPath;
public async void Sorting(DataGridSortingEventArgs e)
{
e.Handled = true;
if (e.Column.SortMemberPath == lastSortMemberPath)
{
if (lastSortDirection == ListSortDirection.Ascending)
e.Column.SortDirection = ListSortDirection.Descending;
else
e.Column.SortDirection = ListSortDirection.Ascending;
}
else
e.Column.SortDirection = ListSortDirection.Ascending;
string dir = (e.Column.SortDirection == ListSortDirection.Ascending) ? "ASC" : "DESC";
lastSortDirection = e.Column.SortDirection;
lastSortMemberPath = e.Column.SortMemberPath;
await UpdateData(e.Column.SortMemberPath, dir);
// In my opinion, this last bit is redundant since data should come already sorted
CollectionView.SortDescriptions.Clear();
CollectionView.SortDescriptions.Add(new SortDescription(e.Column.SortMemberPath, e.Column.SortDirection));
}