Filtering and Sorting a ListView in a UWP – Part 1 – The UX

来源:转载

In my last article, I got the ListView that is driving my task list hooked up to my Task Store by making my Task Store an ObservableCollection. Today I am starting work on the UX for implementing filtering. I want two activities: I want to filter the tasks based on their completed status and I want to be able to sort tasks on something – title, since that’s the only thing I have.

To do this, I need an application bar. Right now, I have a placeholder piece of XML in there. It’s the fourth segment of my UI. Starting from the top, I have the title, the new task box, the list of tasks and finally the app bar. Appbars are relatively easy to produce:

One of the things I try to do is to expand the XAML out. It can always be consolidated later, but breaking it out when you need to is sometimes hard. As a result, this is a longer form than I normally do. If you run this, you will get a light gray bottom panel with an ellipsis on it. Pressing the ellipsis will expand the app bar and show two items on the left – a sort and filter icon. Since I’m using the symbol font, the standard icons are being used.

When I press these icons, I want a menu, but let me think about the menu for a moment. There are two types of menu – a standard flyout allows you to construct the menu yourself with a stack panel and any valid XAML. There is also a MenuFlyout for handling menus. There are MenuFlyoutItem types (a standard one, a separator and a toggle) that can be used to construct the menu. The difference is what happens when a menu item is selected. In the standard flyout, the menu remains open. In the MenuFlyout, the menu closes. If you want the menu to remain open, you must produce your own XAML code. If you are ok with it being closed automatically, then you can use a MenuFlyout. A MenuFlyout would look like this:

This menu contains one item. If I click on the Completed icon, it gets a check mark next to it and the menu is closed. This is great for menus that select new pages, but it is really a bad experience for menus that set or clear settings. I want a standard flyout here. Here is my XAML code that includes the flyout:

Clicking anywhere outside the menu will close the menu in both cases. It’s just a case of the action when a menu option is chosen. In this case, I’ve got a checkbox for the filter and I’ve got a radio button group for the sort mechanism. I’m using the standard styling here (I’ll do another blog post on styling when I work out how to do it), so the controls are clearly form controls.

Now for the code. In MainPage.xaml.cs– the code behind file for the XAML – I’ve defined an enum for the SortMethod:

enum SortMethod { Unsorted, ByTitle }

I’ve also defined two private variables in the MainPagepartial class. The default values reflect the default values in the XAML:

I could easily have a property that just returns these from the UI, but I think it’s more obvious when reading the code what is happening when I use a private variable intermediary. Right now, all I am doing is to change the private variables. These will be used by the filtered list, but that’s my next blog post. Until then, you can find this code in my GitHub Repository.