c# - Windows Phone 8 bind to string resource with format -


my localized resource string, named textresource has value: text: {0}. {0} placeholder string.format.

my user control has dependecyproperty called count.

i bind count text of text box apply localized string. content of text block text: 5 (assuming value of count 5)

i managed figure out how bind localized string

  <textblock text="{binding path=localizedresources.textresource, source={staticresource localizedstrings}}" /> 

or property value

 <textblock text="{binding path=count}" /> 

but not both simultaneous.

how can in xaml?

ps: 1 option add 2 text blocks instead of 1 not sure if practice.

you have 3 options here.

first option: modify view model expose formatted string , bind that.

public string countformatted {   {      return string.format(appresources.textresource, count);   } } 
<textblock text="{binding path=countformatted}" /> 

second option: make converter mycountconverter

public class mycountconverter: ivalueconverter {   public object convert(object value, type targettype, object parameter, system.globalization.cultureinfo culture) {     if (value == null)       return value;      return string.format(culture, appresources.textresource, value);   }    public object convertback(object value, type targettype, object parameter, system.globalization.cultureinfo culture) {     throw new notimplementedexception();   } } 
<phone:phoneapplicationpage.resources>     <local:mycountconverter x:key="mycountconverter"/> </phone:phoneapplicationpage.resources> ... <textblock text="{binding count, converter={staticresource mycountconverter}}"/> 

third option: use bind-able converter parameter can make general stringformat converter can bind converter parameter. not supported out of box in windows phone still doable. check this link on how can done.

however, unless using resources support multiple languages it's easier pass format plain string converter.

<textblock text="{binding count,                    converter={staticresource stringformatconverter},                    converterparameter='text: {0}'}" /> 

you'll have make stringformatconverter converter uses parameter in case.

edit:

regarding third option, can use imultivalueconverter in link above achieve want. can add following converter:

public class stringformatconverter: imultivalueconverter {   public object convert(object[] values, type targettype, object parameter, system.globalization.cultureinfo culture) {     var param = values[0].tostring();     var format = values[1].tostring();      return string.format(culture, format, param);   }    public object[] convertback(object value, type[] targettypes, object parameter, system.globalization.cultureinfo culture) {     throw new notimplementedexception();   } } 
<textblock text="{binding elementname=multibinder, path=output}" />  <binding:multibinding x:name="multibinder" converter="{staticresource stringformatconverter}"     numberofinputs="2"     input1="{binding path=count, mode=twoway}"     input2="{binding path=localizedresources.textresource, source={staticresource localizedstrings}}" /> 

i don't know if it's worth effort though.


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

javascript - jQuery .height() return 0 when visible but non-0 when hidden -