【问题标题】:Xamarin Listview Binding not workingXamarin Listview 绑定不起作用
【发布时间】:2016-11-07 03:02:55
【问题描述】:

我试图让简单的绑定在 Xamarin Listview 上工作,但没有任何运气。 我没有收到任何错误。我刚得到一个 ListView,大概有 10 行左右的空行。

这是我正在使用的 XAML 代码。

  <ContentView.Content >
<StackLayout>      
  <ListView x:Name="listView" ItemsSource="{Binding myItems}" >
    <ListView.ItemTemplate>
      <DataTemplate>

        <ViewCell>
          <StackLayout>
            <Label Text="{Binding TName}"/>
            <Label Text ="{Binding Num, StringFormat='${0}'}"/>
            </StackLayout>                        
        </ViewCell>
      </DataTemplate>
    </ListView.ItemTemplate>
  </ListView>
</StackLayout>

后面的代码

public partial class TestView : ContentView
{
    public TestView()
    {
        InitializeComponent();
        listView.BindingContext = this;
    }

    public static ObservableCollection<Test> myItems
    {
        get
        {
            return new ObservableCollection<Test>(new[] 
            {
                new Test() { TName = "Item 1", Num = 1 },
                new Test() { TName = "Item 2", Num = 2 },
                new Test() { TName = "Item 3", Num = 3 },
                new Test() { TName = "Item 4", Num = 4 },
            });
        }
    }
}

public class Test
{
    public string TName { get; set; }
    public int Num { get; set; }
}

我开始相信 Xamarin 不是我的朋友 :(.

【问题讨论】:

  • this.BindingContext = this;
  • 谢谢杰森!我必须添加它并更改从 myItems 中删除静态。如果你把它作为答案,我会标记它。

标签: listview xamarin


【解决方案1】:

更改如下,它正在工作

    public ObservableCollection<Test> myItems
    {
        get
        {
            return new ObservableCollection<Test>(new[]
            {
            new Test() { TName = "Item 1", Num = 1 },
            new Test() { TName = "Item 2", Num = 2 },
            new Test() { TName = "Item 3", Num = 3 },
            new Test() { TName = "Item 4", Num = 4 },
            }
            );
        }
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-21
    • 2020-11-26
    • 2019-12-13
    • 1970-01-01
    • 2013-07-17
    • 1970-01-01
    • 2017-07-22
    • 1970-01-01
    相关资源
    最近更新 更多