簡體   English   中英

在運行時綁定 Xamarin Forms 中標簽的 FormattedString 跨度

[英]Bind a span of a FormattedString of a Label in Xamarin Forms at runtime

在 Xamarin Forms 中,我想在 collectionView 中創建格式化標簽。 標簽將根據 ItemArray 中的許多值創建。 如果我使用 xaml 在設計中創建標簽,它工作正常:

<Label  VerticalOptions="CenterAndExpand" HorizontalOptions="CenterAndExpand">
<Label.FormattedText>
<FormattedString>
<Span Text="{Binding ItemArray[0]}"  TextColor="Blue"/>
<Span Text="{Binding ItemArray[1]}"  TextColor="Red"/>
<Span Text="{Binding ItemArray[2]}"  TextColor="Blue"/>
</FormattedString>
</Label.FormattedText>

當我嘗試在運行時創建標簽時,我得到的文本是“{Binding ItemArray[0]}”而不是實際值

StackLayout ST2 = new StackLayout { Orientation = StackOrientation.Horizontal };
Label LB1 = new Label
{
FormattedText =
new FormattedString
{
Spans =
{
new Span { Text = "{Binding ItemArray[0], Mode=OneWay}", ForegroundColor = Color.Blue },
new Span { Text = "{Binding ItemArray[1], Mode=OneWay}", ForegroundColor = Color.Red },
new Span { Text = "{Binding ItemArray[2], Mode=OneWay}", ForegroundColor = Color.Blue }
}
}
};

ST2.Children.Add(LB1);

使用設置SetBinding

var span = new Span() { ForegroundColor = Color.Blue };
span.SetBinding (Span.TextProperty, "ItemArray[0]");

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM