我有一个 ListView
,其 ItemSource
绑定(bind)到 ArticleList
。我需要将其中 Label
的 FontSize
绑定(bind)到 MyFontSize
,而 MyFontSize
位于 ArticleList< 内NOT/
。它只是我的 View 模型中的另一个属性,就像 ArticleList
XAML 代码:
<ListView ItemsSource="{Binding ArticleList}"
x:Name="ArticleListView" HasUnevenRows="True"
SeparatorVisibility="None">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<ViewCell.View>
<ContentView>
<StackLayout Padding="10,0,20,10">
<Image Source="{Binding _Image}"/>
<Label Text="{Binding _Description}"
FontSize="{Binding MyFontSize}"/>
</StackLayout>
</ContentView>
</ViewCell.View>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
最佳答案
您需要“定位”为 ViewModel 保存正确数据上下文的元素。我猜你把它连接到你的页面了吗?无论如何,语法如下 - elementname 是具有正确数据上下文的元素。
{Binding DataContext.MyFontSize, ElementName=LayoutRoot}
Dushyant Bangal 的伟大收获,似乎在 Xamarin 中您需要使用 Source 属性才能使绑定(bind)正常工作:)
FontSize="{Binding BindingContext.MyFontSize, Source={Reference LayoutRoot}}"
关于xaml - 如何在ListView中绑定(bind)Label的FontSize,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32176888/