在XAML中纯粹地连接文字和静态资源字符串

3
我尝试了这个:
Sets literally Show {Binding Source={StaticResource AppInfoer}, Path=Title}
<MenuItem Header="Show {Binding Source={StaticResource AppInfoer}, Path=Title}" 
Command="{StaticResource ShowWindowCommand}" CommandParameter="Open" />

给定 Header="{Binding Source={StaticResource AppInfoer}, Path=Title}" 很好地解析为 "Main App",如何成功地

  • {Binding Source={StaticResource AppInfoer}, Path=Title}的字符串输出前添加字面字符串"Show "

我希望有一种没有额外的代码后台解决方案, 只涉及一两行xaml

1个回答

2
你尝试过使用StringFormat吗?
{Binding Source={StaticResource AppInfoer}, Path=Title, StringFormat=Show {0}}

谢谢,但是当我将你的代码复制到菜单项标题中时,它只显示“Main App”,而不是在前面加上“Show”... - Cel
也许 MenuItemHeader 不支持 StringFormat?同时在使用 {} 和单引号时没有成功,参考链接:http://elegantcode.com/2009/04/07/wpf-stringformat-in-xaml-with-the-stringformat-attribute/ 和 https://dev59.com/BHA75IYBdhLWcg3wrbG_。 - Cel
1
最终,使用替代实现 internal class PrependConverter : IValueConverter { public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { return string.Format("Show {0}", value); } //... } 并在 XAML 中添加 <tn:PrependConverter x:Key="PrependConverter"/> 以及 <MenuItem Header="{Binding Source={StaticResource AppInfoer}, Path=Title, Converter={StaticResource PrependConverter}}"> - Cel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接