我需要綁定TextColor
的Label
。但Label
是與SelectedArticle
BindingContext
,因此綁定是不與任何工作以外的SelectedArticle
的綁定(我錯了嗎?)使用BindingContext將StackLayout中Label的TextColor綁定
public Color ArticleFontColor { get; set; }
<StackLayout BindingContext="{Binding SelectedArticle}">
<Label Text="{Binding Title}" FontSize="Large"
TextColor="{Binding ArticleFontColor}"
FontAttributes="Bold"></Label>
</StackLayout>
思考,我嘗試了StackLayout
內使用樣式,但該值不會綁定。
<ContentPage.Resources>
<ResourceDictionary>
<Style x:Key="labelStyle" TargetType="Label">
<Setter Property="TextColor" Value="{Binding ArticleFontColor}" />
</Style>
</ResourceDictionary>
</ContentPage.Resources>
<Label Text="{Binding Title}" FontSize="Large"
Style="{StaticResource labelStyle}"
FontAttributes="Bold"></Label>
TextColor
可以改變運行時,這就是爲什麼我需要結合
'ArticleFontColor'是'Color' 另外,我的第一個代碼片段是您的第一選擇。但它沒有工作,可能是因爲它的父'StackLayout'有''SelectedArticle'的'BindingContext'。 'ArticleFontColor'不是一個屬性,它顯示'SelectedArticle'。那就是爲什麼這個問題。 –