Reputation: 131
I binded the item source and the data. The data does change but the UI does not reflect the changes.
I noticed whenever TestUpdateCell
is called, SetProperty(ref items, value)
is not call either. But if I add this statements at the end of TestUpdateCell
:
var temp = Items;
Items = new List<Item>();
Items = temp;
SetProperty(ref items, value)
is call but the UI does not reflect the changes.
Also, I tried using ObservableCollection instead of List and using Xamarin's documentation https://developer.xamarin.com/guides/xamarin-forms/xaml/xaml-basics/data_bindings_to_mvvm/
Edited to include solution(solution proposed by Gerald Versluis)
Solution: Item class
public class Item : MvvmHelpers.ObservableObject
{
string name;
public string Name
{
get { return name; }
set { SetProperty(ref name, value); }
}
}
Problem: Item class:
public class Item : MvvmHelpers.ObservableObject
{
public string Name { get; set; }
}
Item data class:
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"
});
}
}
View Model:
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";
}
}
Item page 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
MyItemView:
<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>
Upvotes: 0
Views: 1034
Reputation: 34013
If I see correctly, you have now implemented the property changed mechanism on the list with items. While you want to change a value within a item.
Implement the property changed (also) in the Item
class.
If you want to have cells being added and removed dynamically, put them in a ObservableCollection
. But again; it is important to note here; that only observes changes in the list itself, not inside the list items.
Upvotes: 1