c# - How to determine the clicked item in a gridview -
i used xaml code create gridview , add items it:
<grid x:name="mainstack" horizontalalignment="left" height="628" grid.row="1" verticalalignment="top" width="1366"> <gridview x:name="itemgridview" automationproperties.automationid="itemsgridview" automationproperties.name="items" tabindex="1" grid.rowspan="2" padding="130,42,116,46" itemtemplate="{staticresource standard250x250itemtemplate}" selectionmode="none" isswipeenabled="false" isitemclickenabled="true" background="#fffb0404" margin="0,0,0,0" rendertransformorigin="0.489,0.503" itemclick="mainpage_click"> <x:string>item 1</x:string> <x:string>item 2</x:string> <x:string>item 3</x:string> <x:string>item 4</x:string> <x:string>item 5</x:string> <x:string>item 6</x:string> <x:string>item 7</x:string> <x:string>item 8</x:string> </gridview> </grid>
then tried using c# code
private void mainpage_click(object sender, itemclickeventargs e) { string output = e.clickeditem.tostring(); int clickvalue = convert.toint32(output); if (clickvalue == 0) { this.frame.navigate(typeof(itemspagerss)); } }
to make app navigate page when 1st item clicked gave error.
please if can tell me how isolate each iem , add seperate click event each , how customize gridview items adding titles, subtitles , images items. thank in advance.
here giving simple example if can't understand msdn sample. create new project , paste whole code.
xaml
<grid background="{staticresource applicationpagebackgroundthemebrush}"> <gridview x:name="itemgridview" itemtemplate="{staticresource standard250x250itemtemplate}" selectionmode="none" isitemclickenabled="true" background="#fffb0404" itemclick="mainpage_click"/> </grid>
c#
protected override void onnavigatedto(navigationeventargs e) { var gvdata = new observablecollection<mydata>(); (int = 0; < 8; i++) { gvdata.add(new mydata { id = i, image = new bitmapimage(new uri("ms-appx:///assets/logo.png")), //you can add image title = "title " + i, subtitle = "subtitle " + }); } itemgridview.itemssource = gvdata; } private void mainpage_click(object sender, itemclickeventargs e) { mydata output = e.clickeditem mydata; int clickvalue = output.id; if (clickvalue == 0) { this.frame.navigate(typeof(itemspagerss)); //todo : whatever want } //..... //todo : operations other items well. }
you may create mydata
class outside scope of mainpage
(xaml page) class.
public class mydata { public int id { get; set; } public imagesource image { get; set; } public string title { get; set; } public string subtitle { get; set; } }
Comments
Post a Comment