c# - xamarin.forms - uniquely identify controls in listview in XAML -


i new xamarin.forms

i have listview filled backend c# code.

i have 1 label , 1 image defined in it. , have given x:name property in xaml.

now, question want access 2 labels in backend c# code. both labels not accessible because in listview. if put label outside listview, can access in code.

please avoid syntax errors. code works fine. want access these elements can change style phone , tablet.

my xaml code :

// ... <listview x:name="dentistlist">    <listview.itemtemplate>       <datatemplate>          <viewcell>            <image source="{binding imagepath}" x:name="doctorimage"/>            <label text="{binding name}" x:name="doctorname" />          </viewcell>       </datatemplate>    </listview.itemtemplate> </listview> 

my c# code :

 ......  dentistlist.itemsource = new list<doctor>  {         // list of items defined here like...         name = "abc",         imagepath = "img1.jpg"          // etc...  }; 

now, below list, want change style(like, fontsize etc...) of label , image. can not access them.

i tried access them findbyname() method not that.

so, can please answer ?

thank in advance.

from jason's comment, can change label.fontsize , label.textcolor using onidiom so:

<label text="{binding name}" x:name="doctorname">     <label.textcolor>         <onidiom x:typearguments="color"                  phone="yellow"                  tablet="blue"/>     </label.textcolor>      <label.fontsize>         <onidiom x:typearguments="namedsize"                  phone="small"                  tablet="large"/>     </label.fontsize> </label> 

*edit: example using regular integer:

<label.fontsize>     <onidiom x:typearguments="x:double"              phone="20"              tablet="30"/> </label.fontsize> 

*edit #2: if plan use label.fontsize , label.textcolor on multiple pages suggest adding values app.xaml , referencing them contentpages (you add value contentpage's resourcedictionary if using values multiple times on single page):

app.xaml:

<application xmlns="http://xamarin.com/schemas/2014/forms"              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"              x:class="myapp.app">   <application.resources>     <resourcedictionary>        <color x:key="mytextcolor">         <color.accent>           <onidiom x:typearguments="color"                    phone="yellow"                    tablet="blue"/>         </color.accent>       </color>        <x:double x:key="myfontsize">         <onidiom x:typearguments="x:double"                  phone="20"                  tablet="30"/>       </x:double>      </resourcedictionary>   </application.resources> </application> 

now in contentpages:

<label x:name="doctorname"        text="{binding name}"        textcolor="{staticresource mytextcolor}"        fontsize="{staticresource myfontsize}"/> 

if want use namedsize instead might need use converter have seen on xamarin forums. let me know if cannot find , can try around.


Comments