2010-01-14 72 views
3

我有2個字段需要格式化爲TextBlock,例如:「{0}使用{1}小時」。在XAML的一個TextBlock中格式化多個綁定字段

目前有:

<TextBlock Text="{Binding HoursEntered}" /> 
<TextBlock Text=" of " /> 
<TextBlock Text="{Binding EstimatedHours}" /> 
<TextBlock Text=" hours used " /> 

一直在尋找的StringFormat單個字段,但是這似乎只適用於WPF,不Silverlight的:

<TextBlock Text="{Binding Path=HoursEntered, StringFormat='{0} of XX hours used'}"/> 

我想使用MultiBinding但這在Silverlight 3中不可用?

如何在Silverlight 3 xaml中使用多個綁定字段執行格式化字符串?

回答

2

你可以把文本的只讀字符串中的綁定源

Public ReadOnly Property HoursUsedMessage() As String 
    Get 
     Return String.Format("{0} of {1} hours used", _hoursEntered, _estimatedHours) 
    End Get 
End Property 

只是確保你也提高物業通知該物業在HoursEntered和EstimatedHours制定者

+0

此代碼的位置在哪裏?數據來自RIA DomainDataSource。這是否被添加到客戶端或服務器代碼中? – 2010-01-15 01:20:08

+0

這將是您綁定到的對象上的屬性。 – kenwarner 2010-01-15 02:21:35

+0

該對象來自實體框架。如上所述添加了部分類和屬性。作品。 – 2010-01-15 03:25:28

0

如果你想要一個更動態解決方案,您可以使用轉換器。我做了個小例子,看下面的鏈接。爲簡潔起見,我使用了元素綁定,但它適用於任何數據綁定。

爲Silverlight 4

http://pastebin.com/f4465f5ae

0

更新:你現在可以使用的String.Format選項。

<Button Content=」{Binding username, StringFormat=’Log Out of \{0\} Account’}「/>