Gerade Geldenhuys
Gerade Geldenhuys

Reputation: 139

ViewCell not binding

I have the following listView with binding on my xaml page. I am binding data to it from my viewModel. But the values are not binding to the cell.

 <ListView x:Name="historyList" ItemsSource="{Binding History}" HasUnevenRows="true">
    <ListView.ItemTemplate>
      <DataTemplate>
        <ViewCell>
          <ViewCell.View>
            <StackLayout Orientation="Horizontal" Padding="10, 5, 10, 5" Spacing="0" HorizontalOptions="FillAndExpand" >
              <Label Text="{Binding History.DayNumber}" HorizontalOptions="Center" FontSize="15" TextColor="Red" VerticalOptions="Center"></Label>
            </StackLayout>
          </ViewCell.View>
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>

My History object has the property DayNumber, but the value is not binding to the label?

Any suggestion? or am i missing something?

Thank you.

Edit:

Here is how I get my History List:

 void GetAllHistory()
    {
        foreach(CheckInTimeHistory h in App.Database.GetHistoryCheckins(null))
        {
            CheckInHistoryItem hi = new CheckInHistoryItem();

            hi.CheckedIn = (h.AutoCheckIn ? false : true);
            hi.CheckInTime = h.CheckInTime.AddSeconds(-h.CheckInTime.Second).ToString("HH:mm-ss tt");
            hi.DayNumber = h.CheckInTime.Day.ToString();
            hi.DayText = h.CheckInTime.DayOfWeek.ToString().Substring(0, 2);

            History.Add(hi);
        }
    }


    public class CheckInHistoryItem
    {
        public String DayText, DayNumber, CheckInTime;
        public Boolean CheckedIn;
    }

Upvotes: 0

Views: 1283

Answers (2)

Sven-Michael St&#252;be
Sven-Michael St&#252;be

Reputation: 14760

You will see warnings like this in your debug output:

Binding: 'DayNumber' property not found on '....CheckInHistoryItem', target property: 'Xamarin.Forms.Label.Text'

The properties have to be C# properties. Just change your class to

public class CheckInHistoryItem
{
    public String DayText { get; set; }
    public String DayNumber { get; set; }
    public String CheckInTime { get; set; }
    public Boolean CheckedIn { get; set; }
}

and use DayNumber instead of History.DayNumber

<Label Text="{Binding DayNumber}" HorizontalOptions="Center" FontSize="15" TextColor="Red" VerticalOptions="Center"></Label>

Upvotes: 6

SushiHangover
SushiHangover

Reputation: 74209

Assuming History is a static var:

ItemsSource="{x:Static local:History}">

And change:

{Binding History.DayNumber}

to:

{Binding DayNumber}

Binding Basics:

https://developer.xamarin.com/guides/xamarin-forms/user-interface/xaml-basics/data_binding_basics/#Bindings_and_Collections

Upvotes: 0

Related Questions