我试图在XAML中将两个字符串连接为Button
内容。但它不起作用。我尝试过如下:
<Button Content=""String1"+{DynamicResource String2}"/>
使用Multibinding概念。 this它会帮助你。
XAML中的代码如下:
<Button>
<TextBlock>
<Run Text="String1"/>
<Run Text="{DynamicResource String2}"/>
</TextBlock>
</Button>
你可以使用MultiBinding
和StringFormat
参数:
<Button>
<MultiBinding StringFormat="{}{0}{1}">
<Binding>
<Binding.Source>
<sys:String> String1 </sys:String>
</Binding.Source>
</Binding>
<Binding Path="{DynamicResource String2}" />
</MultiBinding>
</Button>
处理这个问题的一种简单而不可扩展的方法是:
<Window x:Class="AdHocWPF.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:System="clr-namespace:System;assembly=mscorlib"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<System:String x:Key="String2">String2</System:String>
</Window.Resources>
<Grid>
<Button>
<TextBlock>
<System:String>String1 </System:String>
<TextBlock Text="{DynamicResource String2}"/>
</TextBlock>
</Button>
</Grid>
</Window>
TextBlocks基本上都是小流量文档,所以它们非常灵活。
如何将内容绑定到视图模型或模型中的值,然后您可以更轻松地将其设置为动态所需的内容,并对其进行更多控制。当然你必须使用MVVM ..
然而,2kay的答案更好......