在我的WPF應用程序,我想展示的東西,看起來像這樣:如何使格式化的TextBlock寬度數據綁定可本地化?
用戶鮑勃已在22:17註銷。
其中「Bob」和「22:17」是數據綁定值。
最明顯的方法,這樣做將是使用StackPanel
多TextBlock
孩子,他們中的一些數據綁定:
<StackPanel Orientation="Horizontal">
<TextBlock Text="The user"/>
<TextBlock Text="{Binding Path=Username}" TextBlock.FontWeight="Bold" />
<TextBlock Text="has logged off at"/>
<TextBlock Text="{Binding Path=LogoffTime}" TextBlock.FontWeight="Bold" />
</StackPanel/>
這工作,但它的醜陋。該程序應該被本地化爲不同的語言,並且爲「用戶」和「已註銷」分隔字符串是本地化災難的接受者。
理想情況下,我願做這樣的事情:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{}The user <Bold>{0}</Bold> has logged off at <Bold>{1}</Bold>">
<Binding Path="Username" />
<Binding Path="LogoffTime" />
</MultiBinding>
</TextBlock>
因此翻譯將看到一個完整的句子The user <Bold>{0}</Bold> has logged off at <Bold>{1}</Bold>
。但是,這當然不起作用。
這是一個常見問題,對此有什麼正確的解決方案?