【问题标题】:ViewCell not bindingViewCell 未绑定
【发布时间】:2016-08-25 13:38:01
【问题描述】:

我的 xaml 页面上有以下带有绑定的 listView。我正在从我的 viewModel 将数据绑定到它。但是这些值没有绑定到单元格。

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

我的 History 对象具有属性 DayNumber,但该值未绑定到标签?

有什么建议吗?还是我错过了什么?

谢谢。

编辑:

这是我获取历史列表的方式:

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

【问题讨论】:

  • History 是静态变量吗?它是如何定义的?
  • History 是可观察的集合吗?
  • Hstory是什么类型的?

标签: mvvm xamarin xamarin.forms


【解决方案1】:

您将在调试输出中看到类似这样的警告:

绑定:在“....CheckInHistoryItem”上找不到“DayNumber”属性,目标属性:“Xamarin.Forms.Label.Text”

属性必须是 C# 属性。只需将您的课程更改为

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

并使用DayNumber 而不是History.DayNumber

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

【讨论】:

    【解决方案2】:

    假设History 是一个静态变量:

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

    然后改变:

    {Binding History.DayNumber}
    

    到:

    {Binding DayNumber}
    

    绑定基础:

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

    【讨论】:

      猜你喜欢
      • 2017-06-18
      • 2021-04-05
      • 1970-01-01
      • 2019-01-29
      • 2017-11-21
      • 2018-11-15
      • 2021-11-26
      • 2019-02-25
      • 2016-12-20
      相关资源
      最近更新 更多