Stringformat连接数据绑定和资源的值

Lou*_*uro 13 wpf binding concatenation string-formatting staticresource

我想在我的窗口标题中连接我的viewmodel中的属性和来自资源文件的值.这是我没有来自资源的字符串工作:

Title="Binding Path=Description, StringFormat=Building: {0}}"
Run Code Online (Sandbox Code Playgroud)

现在我想删除"Building"字符串,并从我在其他地方使用的资源中输入一个值:

xmlns:res="clr-namespace:Project.View.Resources"
{res:Strings.TitleDescription}
Run Code Online (Sandbox Code Playgroud)

我怎样才能定义两者?我可以定义为{1}参数吗?

mad*_*dd0 18

是的你可以.只需使用一个MultiBinding.

MSDN上的文章StringFormat有一个例子.

在您的情况下,代码看起来像这样:

  <TextBlock>
    <TextBlock.Text>
      <MultiBinding  StringFormat="{}{0} {1}">
        <Binding Source="{x:Static res:Strings.TitleDescription}"/>
        <Binding Path="Description"/>
      </MultiBinding>
    </TextBlock.Text>
  </TextBlock>
Run Code Online (Sandbox Code Playgroud)


Mik*_*chs 15

我现在已经MultiBinding在几个地方看到了答案,几乎没有必要使用它.您可以将资源定义为字符串格式,只要只有一个字符串格式参数,MultiBinding就不需要.使代码更简洁:

<TextBlock Text="{Binding Description, StringFormat={x:Static res:Strings.TitleDesc}}" />
Run Code Online (Sandbox Code Playgroud)

TitleDesc资源是明显"Building: {0}".