Dushyant Bangal
Dushyant Bangal

Reputation: 6403

How to bind FontSize of Label inside a ListView

I have a ListView whose ItemSource is binded to ArticleList. I need to Bind the FontSize of the Label inside it to MyFontSize which is NOT inside ArticleList. Its just another property in my view model, just like ArticleList

XAML code:

<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>

Upvotes: 2

Views: 2272

Answers (2)

Depechie
Depechie

Reputation: 6142

You need to 'target' the element which holds the correct datacontext for your ViewModel. I would guess you hooked it up to your page? Anyhow, the syntax is as follows - with elementname the element with the correct datacontext.

{Binding DataContext.MyFontSize, ElementName=LayoutRoot}

Great catch by Dushyant Bangal, seems in Xamarin you need to use the Source property for the binding to work :)

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

Upvotes: 4

Kristian Vukusic
Kristian Vukusic

Reputation: 3324

Try something like this:

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

Upvotes: 0

Related Questions