首页 > 解决方案 > XAML 中的 String.Format 没有绑定

问题描述

我需要像这样连接一些字符串的各种组合:

"After doing this and that, press the 'whatever'-button."

有多种组合可能,为了保证将来的安全,我想做一些

String.Format("After doing this and that, press the '{0}'-button.", "whatever");

使用来自字符串资源的字符串。

我发现的是:

<!-- Strings.Temperature contains: "Temperature: {0} °C" --> 
Text="{Binding Temperature, StringFormat={x:Static loc:Strings.Temperature}}" />

甚至:

<TextBlock.Text>
    <!-- Strings.Temperature contains: "Temperature: {0} °C / {1} °F" --> 
    <MultiBinding StringFormat=StringFormat={x:Static loc:Strings.Temperature}>
        <Binding Path="{Binding TemperatureCelcius}" />
        <Binding Path="{Binding TemperatureFahrenheit}" />
    </MultiBinding>
</TextBlock.Text>

但我没有绑定到“值”字符串。它也是一个静态变量。而且我知道我真的可以将它绑定到一些视图模型字符串属性,但我不想在不同的视图模型中产生这么多的开销。

有任何想法吗?

标签: wpfxaml

解决方案


推荐阅读