StringFormat 被忽略
这是我的绑定(缩短,命令属性也被绑定)
<MenuItem Header="Key" CommandParameter="{Binding StringFormat='Key: {0}', Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
ContectMenu 的 PlacementTarget 的标签属性是一个字符串,就像
"Short.Plural"
我期望在命令处理程序中收到的是:
Key: Short.Plural
但我实际收到的是:
Short.Plural
This is my binding (shortened, Command-Property is also bound)
<MenuItem Header="Key" CommandParameter="{Binding StringFormat='Key: {0}', Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
The Tag-Property of ContectMenu's PlacementTarget is a String like
"Short.Plural"
What i expect to receive in the Command-Handler is:
Key: Short.Plural
But what i acutally receive is:
Short.Plural
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Label 不使用 StringFormat,而是使用 ContentStringFormat。这样使用它:
Label does not use StringFormat but ContentStringFormat. Use it this way:
我很惊讶,但我的测试仅表明
StringFormat
仅适用于目标 d-prop 的类型为String
的情况。我以前从未注意到这一点,也没有听人提起过。我现在没有更多的时间去研究它,但这看起来很荒谬。说真的,这可行:
这不行:
因为
Label.Content
不是String
。I'm astounded, but my tests just show that
StringFormat
only applies if the target d-prop is of typeString
. I've never noticed this before, nor heard it mentioned. I don't have more time to look into it right now, but this seems ridiculous.Seriously, this works:
This does not:
Since
Label.Content
is not aString
.使用 Binding Converter:
将其添加到 Windows\UserControl 资源:
在菜单命令参数绑定中引用它:
use Binding Converter:
Add it to Windows\UserControl resource:
Refer it in Menu CommandParameter binding: