为什么在 Binding 中有效的格式字符串在 MultiBinding 中不起作用?
我对这个问题很感兴趣: MultiBinding StringFormat of TimeSpan
如果我定义了以下绑定其中 StartTime 的类型为 TimeSpan:
<TextBlock Text={Binding Path=StartTime, StringFormat='{}From {0:hh\\:mm}'}" />
上面的绑定按预期计算。但是,正如原始问题中的场景所示,如果我尝试在 MultiBinding 中使用相同的格式字符串,则会失败并出现 FormatException:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}From {0:hh\\:mm} to {1:hh\\:mm}">
<Binding Path="StartTime" />
<Binding Path="EndTime" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
问题是,有人知道为什么吗?这是错误还是预期行为?对我来说,为了在 MultiBinding 中获得相同的输出,我必须将格式字符串中的“\:”更改为“:”(正如我在回答原始问题时发现的那样),这似乎很奇怪。
I was intrigued by this question: MultiBinding StringFormat of TimeSpan
If I have the following Binding defined where StartTime is of type TimeSpan:
<TextBlock Text={Binding Path=StartTime, StringFormat='{}From {0:hh\\:mm}'}" />
The above binding evaluates as expected. However, as the scenario in the original question shows, if I try to use the same format string in a MultiBinding, it fails with a FormatException:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}From {0:hh\\:mm} to {1:hh\\:mm}">
<Binding Path="StartTime" />
<Binding Path="EndTime" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
The question is, does anyone know why? Is this a bug or expected behavior? It seems odd to me that to get the same output in a MultiBinding, I have to change the "\:" to a ':' in the format string (as I discovered in answering the original question).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这似乎是 WPF 4 中的一个错误,如果不是的话,它至少是 WPF 3.5 中的一个重大更改。以下面的代码为例:
后面的代码如下:
如果针对 .NET 3.5 编译并运行此代码,输出(即窗口内容)将如下所示:
采用确切的示例代码/项目并针对 .NET 4 运行它,您将得到:
我找到了一个可能相关的错误报告,但作者从未回复,因此 Microsoft 将问题关闭为“不可重现”。
因此,根据子绑定的使用方式,StringFormat 在 .NET 4 中可能有效,也可能无效。
This appears to be a bug in WPF 4, if not it's at least a breaking change from WPF 3.5. Take the following code for example:
With the code behind like:
If you compile and run this code against .NET 3.5, the output (i.e. Window content) will look like this:
Taking the exact sample code/project and running it against .NET 4 you get:
I found one bug report that may be related, but the author never responded so Microsoft closed the issue as 'Not Reproducible'.
So it appears that depending on the how the child Bindings are used, the StringFormat may or may not work in .NET 4.