2017-02-05 56 views
1

我正在使用FormattedString在Xamarin.Forms上的標籤上顯示自定義文本。我試圖實現的是更改一個或多個元素的顏色,例如:$$ $$。但是,即使我改變顏色的標籤只顯示都具有相同的顏色美元符號:$$$$如何使標籤以不同顏色的字母顯示FormattedString?

這是視圖上的標籤:

<Label Text="{Binding AveragePrice, StringFormat='{0}'}" HorizontalTextAlignment="Center" /> 

這是的代碼屬性綁定到ViewModel上的標籤文本

public FormattedString AveragePrice 
    { 
     get 
     { 
      return new FormattedString 
      { 
       Spans = 
       { 
        new Span { Text = "$", ForegroundColor=Color.Black }, 
        new Span { Text = "$", ForegroundColor=Color.Black }, 
        new Span { Text = "$", ForegroundColor=Color.Gray }, 
        new Span { Text = "$", ForegroundColor=Color.Gray } 
       } 
      }; 
     } 
    } 

爲什麼這段代碼不會改變美元符號的顏色?我該如何做到這一點?

+2

將AveragePrice綁定到FormattedText屬性,並刪除StringFormat。

+0

太棒了!有效。謝謝@BillReiss。您應該發佈答案,將問題標記爲回答 –

+0

ts工作在一個簡單的Label.But相同的代碼不工作,如果我在DataTemplate中加載該標籤。有什麼建議? – Divakar

回答

5

將AveragePrice綁定到FormattedText屬性,並刪除StringFormat。

<Label FormattedText="{Binding AveragePrice}" HorizontalTextAlignment="Center" /> 
相關問題