How do we change the binding property of text block on demand in windows phone 8? -
i'm working on windows phone 8 application, which should work in 2 languages, english , arabic. default language english. later user can change language either english arabic or arabic english in settings page.
when user clicks on "cities" button in home page, i'm displaying cities in listbox. default i'm binding city english value i.e. textblock x:name="city" text="{binding citynameen}". below ui code.
<listbox x:name="citieslist" selectionchanged="citieslist_selectionchanged"> <listbox.itemtemplate> <datatemplate> <grid height="50" margin="0,10,0,0"> <grid.rowdefinitions> <rowdefinition height="40"/> <rowdefinition height="10"/> </grid.rowdefinitions> <stackpanel x:name="datarow" grid.row="0" orientation="horizontal"> <textblock x:name="city" text="{binding citynameen}" foreground="#ff501f6e" style="{staticresource phonetextnormalstyle}" horizontalalignment="left" fontsize="28" width="420"/> <image x:name="arrow" stretch="fill" margin="0,0,0,0" source="images/arrow.png" height="20"/> </stackpanel> <image x:name="line" grid.row="1" width="460" horizontalalignment="center" source="images/separator.png" /> </grid> </datatemplate> </listbox.itemtemplate>
i'm setting list box source below. private void cities_page_loaded(object sender, routedeventargs e) { citieslist.itemssource = constants.cities; }
below data context class of city.
class city { public string cityid { get; set; } public string citynamear { get; set; } public string citynameen { get; set; } public string displayorder { get; set; } public int focusedcityindex { get; set; } }
now when language english, cities displaying in english. because bind text block property citynameen contains english values.
when user changes language in settings page english arabic. implemented localization below.
if (selecteditem.equals("english")) { thread.currentthread.currentculture = new system.globalization.cultureinfo("en-us"); thread.currentthread.currentuiculture = new system.globalization.cultureinfo("en-us"); } else { thread.currentthread.currentculture = new system.globalization.cultureinfo("ar"); thread.currentthread.currentuiculture = new system.globalization.cultureinfo("ar"); }
now when user clicks on cities button in home page, cities sholud display in arabic. *because user changed language english arabic.*
but cities displaying in english because textblock binded property contains english value. *so display cities in arabic, i've change textblock binding property contains arabic value.*
how should change binding property of text block based on language change.
thanks.
you add city.cityname
getter property in city
class return city name based on current culture, this:
class city { public string cityid { get; set; } public string citynamear { get; set; } public string citynameen { get; set; } public string cityname { { if (thread.currentthread.currentculture.name == "en-us") return this.citynameen; else return this.citynamear; } } public string displayorder { get; set; } public int focusedcityindex { get; set; } }
then use property in xaml:
<textblock x:name="city" text="{binding cityname}" foreground="#ff501f6e" style="{staticresource phonetextnormalstyle}" horizontalalignment="left" fontsize="28" width="420"/>
Comments
Post a Comment