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