【问题标题】:How to bind FontSize of Label inside a ListView如何在 ListView 中绑定 Label 的 FontSize
【发布时间】:2015-11-17 13:22:17
【问题描述】:

我有一个ListView,其ItemSource 绑定到ArticleList。我需要将其中的LabelFontSize 绑定到MyFontSize,这是ArticleList 内部的NOT。它只是我视图模型中的另一个属性,就像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>

【问题讨论】:

    标签: xaml listview binding windows-phone-8.1 xamarin.forms


    【解决方案1】:

    您需要“定位”为您的 ViewModel 保存正确数据上下文的元素。我猜你把它连接到你的页面上?无论如何,语法如下 - elementname 具有正确数据上下文的元素。

    {Binding DataContext.MyFontSize, ElementName=LayoutRoot}
    

    Dushyant Bangal 的成功之处,似乎在 Xamarin 中您需要使用 Source 属性才能使绑定工作:)

    FontSize="{Binding BindingContext.MyFontSize, Source={Reference LayoutRoot}}"
    

    【讨论】:

    • ElementName 在 Xamarin.Forms 中不可用。抱歉,我没有指定 Xamarin.Forms。但您确实为我指明了正确的方向。以下对我有用:FontSize="{Binding BindingContext.MyFontSize, Source={Reference LayoutRoot}}"
    • 将你的答案与我的结合,将其选为答案
    • 哈,Xamarin 的“差异”;)
    • 我受够了这些细微的差别。但话又说回来,SourceReferenceElementName 更有意义:)
    【解决方案2】:

    试试这样的:

    FontSize="{Binding ElementName=ArticleListView, Path=DataContext.MyFontSize}"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 2016-06-05
      • 2015-11-15
      • 2017-11-28
      • 2016-12-28
      • 1970-01-01
      相关资源
      最近更新 更多