StringFormat被忽略

Rei*_*ini 21 wpf binding string-formatting string-literals


这是我的绑定(缩短,Command-Property也绑定)

<MenuItem Header="Key" CommandParameter="{Binding StringFormat='Key: {0}', Path=PlacementTarget.Tag, RelativeSource={RelativeSource AncestorType=ContextMenu}}"/>
Run Code Online (Sandbox Code Playgroud)

ContectMenu的PlacementTarget的Tag-Property是一个类似String的属性

"Short.Plural"
Run Code Online (Sandbox Code Playgroud)

我期望在Command-Handler中收到的是:

Key: Short.Plural
Run Code Online (Sandbox Code Playgroud)

但我真正得到的是:

Short.Plural
Run Code Online (Sandbox Code Playgroud)

tab*_*ina 34

Label不使用StringFormat而是使用ContentStringFormat.用这种方式:

<TextBlock x:Name="textBlock" Text="Base Text"/>
<Label Content="{Binding Path=Text, ElementName=textBlock}" ContentStringFormat="FORMATTED {0}"/>
Run Code Online (Sandbox Code Playgroud)

  • 这应该标记为答案.同样,对于MenuItem来格式化Header,它是HeaderStringFormat (6认同)

Ken*_*art 24

我很震惊,但我的测试只表明StringFormat只有在目标d-prop是类型时才适用String.我以前从未注意到这一点,也没有听到过它.我现在没有更多的时间来研究它,但这看起来很荒谬.

说真的,这有效:

<TextBlock x:Name="textBlock" Text="Base Text"/>
<TextBlock Text="{Binding StringFormat=FORMATTED {0}, Path=Text, ElementName=textBlock}"/>
Run Code Online (Sandbox Code Playgroud)

这不是:

<TextBlock x:Name="textBlock" Text="Base Text"/>
<Label Content="{Binding StringFormat=FORMATTED {0}, Path=Text, ElementName=textBlock}"/>
Run Code Online (Sandbox Code Playgroud)

既然Label.Content不是String.