wpf - Why does a format string that works in a Binding not work in a MultiBinding? -
i intrigued question: multibinding stringformat of timespan
if have following binding defined starttime of type timespan:
<textblock text={binding path=starttime, stringformat='{}from {0:hh\\:mm}'}" />
the above binding evaluates expected. however, scenario in original question shows, if try use same format string in multibinding, fails formatexception:
<textblock> <textblock.text> <multibinding stringformat="{}from {0:hh\\:mm} {1:hh\\:mm}"> <binding path="starttime" /> <binding path="endtime" /> </multibinding> </textblock.text> </textblock>
the question is, know why? bug or expected behavior? seems odd me same output in multibinding, have change "\:" ':' in format string (as discovered in answering original question).
this appears bug in wpf 4, if not it's @ least breaking change wpf 3.5. take following code example:
<window x:class="wpfsampletestbed.mainwindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" title="mainwindow" height="350" width="525"> <stackpanel> <textblock text="{binding path=starttime, stringformat='{}from {0:hh\\:mm}'}" /> <textblock x:name="textblock2"> <textblock.text> <multibinding stringformat="{}from {0:hh\\:mm} {1:hh\\:mm}"> <binding path="starttime" /> <binding path="endtime" /> </multibinding> </textblock.text> </textblock> <textblock x:name="textblock3" text="three" /> <textblock x:name="textblock4" text="four" /> <textblock> <textblock.text> <multibinding stringformat="three = {0}, 4 = {1}"> <binding elementname="textblock3" path="text" /> <binding elementname="textblock4" path="text" /> </multibinding> </textblock.text> </textblock> </stackpanel> </window>
with code behind like:
using system; using system.windows; namespace wpfsampletestbed { public partial class mainwindow : window { public mainwindow() { initializecomponent(); this.datacontext = new test() { starttime = timespan.fromseconds(90), endtime = timespan.fromseconds(100), }; } } public class test { public timespan starttime { get; set; } public timespan endtime { get; set; } } }
if compile , run code against .net 3.5, output (i.e. window content) this:
from 00:01:30
00:01:30 00:01:40
3
4
3 = three, 4 = 4
taking exact sample code/project , running against .net 4 get:
from 00:01:30
3
4
3 = three, 4 = 4
i found 1 bug report may related, author never responded microsoft closed issue 'not reproducible'.
so appears depending on how child bindings used, stringformat may or may not work in .net 4.
Comments
Post a Comment