Lgn
Lgn

Reputation: 10099

GridView SelectionMode with maximum 2 selections in Windows 8 Apps

I would like to maximize the item selections in a Gridview, so that the user should select 1 or 2 items. In XAML there are only predefined options in SelectionMode like Multiple, Extended, Single, None. I'm afraid I need another way to maximize the selections. Could you give me a suggestion?

Upvotes: 2

Views: 1908

Answers (1)

nemesv
nemesv

Reputation: 139758

One way to mimic maximum 2 selection is to subscibe on the SelectionChanged event and remove the first/last element from the SelectedItems collection:

XAML:

<GridView SelectionMode="Multiple" SelectionChanged="GridView_SelectionChanged" />

Codebehind:

private void GridView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
    var gridView = sender as GridView;
    if (gridView == null) return;
    if (gridView.SelectedItems.Count > 2)
    {
        gridView.SelectedItems.Remove(gridView.SelectedItems[0]);
    }
}

Upvotes: 4

Related Questions