如何在xaml中连接按钮内容?

问题描述 投票:7回答:5

我试图在XAML中将两个字符串连接为Button内容。但它不起作用。我尝试过如下:

<Button Content=""String1"+{DynamicResource String2}"/>
c# wpf xaml button concatenation
5个回答
2
投票

使用Multibinding概念。 this它会帮助你。


6
投票

XAML中的代码如下:

 <Button>
        <TextBlock>
            <Run Text="String1"/>
            <Run Text="{DynamicResource String2}"/>
        </TextBlock>
 </Button>

2
投票

你可以使用MultiBindingStringFormat参数:

<Button>
    <MultiBinding StringFormat="{}{0}{1}">
        <Binding>
            <Binding.Source>
                <sys:String> String1 </sys:String>
            </Binding.Source>
        </Binding>
        <Binding Path="{DynamicResource String2}" />
    </MultiBinding>
</Button>

1
投票

处理这个问题的一种简单而不可扩展的方法是:

<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基本上都是小流量文档,所以它们非常灵活。


0
投票

如何将内容绑定到视图模型或模型中的值,然后您可以更轻松地将其设置为动态所需的内容,并对其进行更多控制。当然你必须使用MVVM ..

然而,2kay的答案更好......

© www.soinside.com 2019 - 2024. All rights reserved.