Reputation: 720
I have a ListView and wish to sort it up and down. Here is the xaml
<ListView x:Name="ListRegister" ItemsSource="{Binding Registrations}" HasUnevenRows="True" SelectionMode="Single">
<ListView.Behaviors>
<b:EventToCommandBehavior EventName="ItemTapped" Command="{Binding CommandGoDetail}" EventArgsParameterPath="Item" />
</ListView.Behaviors>
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout>
<Frame >
<StackLayout>
<Label Text="{Binding Date}" Style="{StaticResource registerItemDateText}"/>
</StackLayout>
</Frame>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
As well as the "button" (alternates between my SortUp() and sortDown() functions). The button changes a text to "up" and "down" so I know it works on screen.
<TapGestureRecognizer Command="{Binding CommandSort}"/>
These function seem to work
Registrations.Sort((x, y) => DateTime.Compare(y.Date, x.Date));
Registrations.Sort((x, y) => DateTime.Compare(x.Date, y.Date));
The list is correctly sorted Up or Down when one of those functions is called but when the button is Pressed, the list does not updates itself to the other sorting.
Where did I mess up ?
EDIT :
CommandSort code
private void HandleSort(Registration obj)
{
if(SortRecent)
{
SortRecent = false;
SortText = "Plus ancien";
SortImageSource = "lightDownArrow";
SortDown();
}
else
{
SortRecent = true;
SortText = "Plus récent";
SortImageSource = "lightUpArrow";
SortUp();
}
}
Registrations is of type :
List<Registration> Registrations
A Registration having a string name, and a DateTime date
Upvotes: 0
Views: 759
Reputation: 428
You need to check what type of data is Registrations. It must be an Observable Collection in order to inform the UI about the changes made to the model. If not, You will need to manually update the list view to reflect the changes made to the model, in this case, the new sorting direction. You could use :
ListView.BeginRefresh(); & ListView.EndRefresh();
to force the update of the list view. But it is highly recommended to change your list to an observable collection.
Upvotes: 1