【问题标题】:listview cell not updating using data bindinglistview 单元格未使用数据绑定进行更新
【发布时间】:2017-10-12 21:57:50
【问题描述】:

我绑定了项目源和数据。数据确实发生了变化,但 UI 并未反映这些变化。

我注意到每当TestUpdateCell 被调用时,SetProperty(ref items, value) 也不会被调用。但是如果我在TestUpdateCell的末尾添加这个语句:

var temp = Items;
Items = new List<Item>();
Items = temp;

SetProperty(ref items, value) 已调用,但 UI 未反映更改。

另外,我尝试使用 ObservableCollection 而不是 List 并使用 Xamarin 的文档 https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_bindings_to_mvvm/

已编辑以包含解决方案(Gerald Versluis 提出的解决方案)

解决方案:物品类

public class Item : MvvmHelpers.ObservableObject
    {           
        string name;
        public string Name      
        {
            get { return name; }
            set { SetProperty(ref name, value); }
        }
    }

问题:物品类别:

public class Item : MvvmHelpers.ObservableObject
    {
        public string Name { get; set; }
    }

物品数据类:

public class ItemData : List<Item>
{
    public ItemData()
    {
        Add(new Item
        {
            Name = "Item One"
        });

        Add(new Item
        {
            Name = "Item Two"
        });

        Add(new Item
        {
            Name = "Item Three"
        });
    }
}

查看模型:

public class ItemViewModel : MvvmHelpers.BaseViewModel
    {
        public ItemViewModel()
        {
            Items = new ItemData();
        }

        List<Item> items;
        public List<Item> Items
        {
            get { return items; }
            set { SetProperty(ref items, value); }
        }

        public void TestUpdateCell()
        {
            Items[0].Name = "Item One Updated";
        }
    }

物品页面cs:

    public class ItemPage : ContentPage
    {
        ItemViewModel ivm;
        public ItemPage()
        {
            BindingContext = ivm = new ItemViewModel();
        }

        void ItemTapped(object sender, ItemTappedEventArgs e)
        {       
            ivm.TestUpdateCell();
        }
}

itempage.XAML:

ListView
    ItemsSource="{Binding Items}"
    ItemTapped="ItemTapped"  >
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <view:MyItemView/>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView

我的项目视图:

<ContentView
    xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:local="clr-namespace:MyProject;assembly=MyProject"
    x:Class="MyProject.MyItemView">
    <local:CardFrame 
        IsClippedToBounds="True"
        HasShadow="True" >
        <StackLayout 
            Spacing="0"
            Orientation="Horizontal">
            <Grid 
                RowSpacing="0">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <local:MyLabel 
                    Grid.Row="0"
                    Grid.Column="0"
                    Grid.ColumnSpan="4"
                    FontSize="18"
                    FontAttributes="Bold"
                    Text="{Binding Name}"/>
                <!-- omitted rows and columns -->
            </Grid>
        </StackLayout>
    </local:CardFrame>
</ContentView> 

【问题讨论】:

  • 使用 ObservableCollection 代替 List

标签: listview xamarin data-binding xamarin.forms listviewitem


【解决方案1】:

如果我没看错,您现在已经在项目列表上实现了属性更改机制。当你想在一个项目改变一个值时。

Item 类中实现(也)更改的属性。

如果您想动态添加和删除单元格,请将它们放在ObservableCollection 中。但又一次;重要的是要注意这里;只观察列表本身的变化,而不是列表项内部的变化。

【讨论】:

  • 谢谢你,这就是我所缺少的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多