[英]how to Concatenate Button Content in xaml?
I tried to concatenate two string as Button
content in XAML. 我试图在XAML中将两个字符串连接为Button
内容。 But It is not working. 但它不起作用。 I tried like below: 我尝试过如下:
<Button Content=""String1"+{DynamicResource String2}"/>
The code in XAML is the following: XAML中的代码如下:
<Button>
<TextBlock>
<Run Text="String1"/>
<Run Text="{DynamicResource String2}"/>
</TextBlock>
</Button>
You can use MultiBinding
with StringFormat
parameter: 您可以将MultiBinding
与StringFormat
参数一起使用:
<Button>
<MultiBinding StringFormat="{}{0}{1}">
<Binding>
<Binding.Source>
<sys:String> String1 </sys:String>
</Binding.Source>
</Binding>
<Binding Path="{DynamicResource String2}" />
</MultiBinding>
</Button>
A simple, not terribly scalable way to deal with this would be: 处理这个问题的一种简单而不可扩展的方法是:
<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 are basically little flowdocuments, so they're very flexible. TextBlocks基本上都是小流量文档,所以它们非常灵活。
How about binding the content to a value in the view model or model and then you can set this a lot more easily to what you want dynamically and have a lot more control over it. 如何将内容绑定到视图模型或模型中的值,然后您可以更轻松地将其设置为动态所需的内容,并对其进行更多控制。 Of course you'd have to be using MVVM.. 当然你必须使用MVVM ..
however, 2kay's answer is better... 然而,2kay的答案更好......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.