WIN10 Development: Implementing the inverse selection of the GridView selection

Source: Internet
Author: User

Today in the development of the Flickr downloader encountered to reverse the selection of the GridView selection of the problem, took a while to solve, so write a blog post.

I think my way of achieving is very low, really is a very foolish way. No, I'm just a stupid man, I just think of such a foolish way. If the reader has a better way to welcome advice.

A bit more nonsense, get into the chase.

Let's talk about the properties or methods of several related gridview:

Property:

SelectionMode: The selection mode of the item, there are four optional values. The default is radio, which is single. Multi-Select as: multiple



SelectedItem: Gets or sets the selected item to read and write

SelectedItems: Gets the currently selected item, read-only. Because the GridView selected item can be multiple, it needs to be used.

Selectedranges: Gets the range collection for the selected item, and returns a Ireadonlylist<itemindexrange> collection. The itemindexrange here have the following three properties:


Itemindexrange can be understood as an interval, a continuous paragraph.

Method:

void SelectAll (): Selects all the list items. No parameter no return value

void Selectrange (Itemindexrange itemindexrange): selects the corresponding item according to the parameter itemindexrange, no return value

void Deselectrange (Itemindexrange itemindexrange): uncheck the corresponding item according to the parameter itemindexrange, no return value


In fact, after the introduction of the above several properties and methods, I believe many people can think of solutions.

Xaml:

<gridview x:name= "GridView" selectionmode= "multiple" itemssource= "{x:bind items}" >            < gridview.itemtemplate>                <datatemplate x:datatype= "Local:item" >                    <stackpanel Orientation= " Horizontal "  margin=" 2,0,0,0 ">                        <symbolicon symbol=" {x:bind Symbol} "/>                        <textblock text=" {x: Bind Label} "margin=" 24,0,0,0 "verticalalignment=" Center "/>                    </StackPanel>                </DataTemplate>            </GridView.ItemTemplate></GridView>

The items items are given in the C # code:

Private list<item> items = new List<item> ()        {            new Item () {Label = "people", Symbol =  symbol.peopl E  },            new Item () {label = "Globe", symbol = Symbol.globe},            new Item () {label = "Message", symbol = Symbol.me Ssage},            new Item () {label = "Mail", symbol = Symbol.mail},            new Item () {label = "CellPhone", symbol = Symbol.ce Llphone},        };

Item Class:

public class Item    {public        string Label {get; set;}        Public symbol symbol {get; set;}    }

The above is not the focus, look at the following lines of code:

private void Switch_click (object sender, RoutedEventArgs e)      {            var selectedranges = gridview.selectedranges;            Gridview.selectall ();            foreach (var item in selectedranges)            {                gridview.deselectrange (item);            }      }

What, does it feel simple?

However, it is ... I am a good person, even the code to play with me.

The above 6 lines of code does not achieve the effect of anti-election, do not believe you try. I blew it anyway.

After debugging, it was found that the SelectAll () method was executed before and after the selectedranges changed. (The ghost knows why, obviously has already assigned the value.) If anyone knows, please let us know and thank you very much. )

Then came up with a solution to make a copy of the selectedranges, so there is the following method:

private void Switch_click (object sender, RoutedEventArgs e)        {            var selectedranges = gridview.selectedranges;            list<itemindexrange> tempranges = new list<itemindexrange> ();            foreach implementation copy             foreach (var item in selectedranges)            {                tempranges.add (item);            }            Gridview.selectall ();            foreach (var item in tempranges)            {                gridview.deselectrange (item);            }        }

This has finally achieved the desired anti-election effect, really (Yu) chi (chun) such as (zhi) I (JI) Ah ~ ~ ~



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

WIN10 Development: Implementing the inverse selection of the GridView selection

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.