在xaml中绑定后是否可以添加更多字符?

Ter*_*rry 23 wpf xaml binding

我想知道什么,找不到任何相关的主题.我有以下绑定:

Content="{x:Static resx:Resource.Form_OtherOption_Description}"
Run Code Online (Sandbox Code Playgroud)

这将在标签中放置一个字符串.我问自己的是,如果我可以在绑定之后添加":",而不是在代码中,只需在xaml中添加.标签代表"名称:".但添加":"作为绑定的一部分不是一种选择.

编辑

我在3.5版本工作

有什么建议.

提前致谢.

use*_*116 32

您可以通过以下方式实现此目的:

<TextBlock Text="{Binding Source={x:Static resx:Resource.Form_OtherOption_Description},
                         StringFormat={}{0}:}" />
Run Code Online (Sandbox Code Playgroud)

编辑: <Label> s Content属性显然不尊重StringFormat绑定的属性.我发现的已被移至该ContentStringFormat物业<Label>.

<Label Content="{x:Static resx:Resource.Form_OtherOption_Description}"
       ContentStringFormat="{}{0}:" />
Run Code Online (Sandbox Code Playgroud)

  • 看来`Label.Content`没有从绑定中获取`StringFormat`,因为`TargetType`不是`string`,而是`object`.在`TextBlock.Text`的情况下,目标类型是`string`,因此它在绑定中使用. (2认同)

Naw*_*waz 19

如果您使用的是WPF 4.0,也可以这样做:

<TextBlock>
       <Run Text="{Binding SomeLabel}"/>
       <Run Text=":"/>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

这实际上连接了来自两个Run标签的两个字符串并复制到TextBlock.Text属性!

使用此方法,您甚至可以绑定到演示者中的不同属性,并将其显示在单个属性中TexBlock.看到这个很好的例子:

我们可以在数据绑定中连接两个属性吗?


And*_*rew 10

您还可以将MultiBinding与StringFormat一起使用,例如:

<TextBlock>
    <TextBlock.Text>
        <MultiBinding StringFormat="ID {0} Name: {1} Age: {2}">
            <Binding Source="{x:Static resx:SomeResx.ID}"/>
             <Binding Path="Name"/>
             <Binding Path="Age"/>
        </MultiBinding>
    </TextBlock.Text>
</TextBlock>
Run Code Online (Sandbox Code Playgroud)

你可以在内容控件TextBlock TextBlock.Text中使用它(抱歉,我无法获得上面显示的代码)


rez*_*e08 6

是的你可以。这里我在windows手机中绑定文本(clouds.all)后添加“测试”。

<TextBlock Text="{Binding Path=clouds.all, StringFormat=\{0\}testing}"/>
Run Code Online (Sandbox Code Playgroud)