wpf - How can I make a formatted TextBlock width data binding localizable? -
in wpf application, display looks this:
user bob has logged off @ 22:17.
where "bob" , "22:17" data-bound values.
the obvious way use stackpanel
multiple textblock
children, of them data bound:
<stackpanel orientation="horizontal"> <textblock text="the user"/> <textblock text="{binding path=username}" textblock.fontweight="bold" /> <textblock text="has logged off at"/> <textblock text="{binding path=logofftime}" textblock.fontweight="bold" /> </stackpanel/>
this works, it's ugly. program supposed localized different languages, , having separate strings "the user" , "has logged off at" recipie localization disaster.
ideally, this:
<textblock> <textblock.text> <multibinding stringformat="{}the user <bold>{0}</bold> has logged off @ <bold>{1}</bold>"> <binding path="username" /> <binding path="logofftime" /> </multibinding> </textblock>
so translator see complete sentence the user <bold>{0}</bold> has logged off @ <bold>{1}</bold>
. doesn't work, of course.
this has common problem, what's right solution this?
i've never tried before, if had try , use converter takes multibinding , breaks , returns stackpanel of pieces
for example, binding like:
<label> <label.content> <multibinding converter={staticresource textwithboldparametersconverter}> <binding source="the user {0} has logged off @ {1}" /> <binding path="username" /> <binding path="logofftime" /> </multibinding> </label.content> </label>
and converter like
public class textwithboldparametersconverter: imultivalueconverter { public object convert(object[] values, type targettype, object parameter, cultureinfo culture) { // create stackpanel hold content // set stackpanel's orientation horizontal // take values[0] , split {x} tags // go through array of values parts , create textblock object each part // if part {x} piece, use values[x+1] text , make textblock bold // add textblock stackpanel // return stackpanel } }
Comments
Post a Comment