Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have been successfully adding an item to list in a MVVM, and now my problem is maintaining the list in the view model. Every time i navigate to a page or go back to a page and return to that listview, the list resets. how will i able to achieve that? i am currently using the prism to build the MVVM.

The ViewModel:

XML
public ObservableCollection<CartData> _cartData;

public ObservableCollection<CartData> CartData
        {

         get {

               if (_cartData == null)
                {
                    _cartData = new ObservableCollection<CartData>();
                }
             return _cartData;
             }
        set {

                SetProperty(ref _cartData, value);
           }
        }




        private DelegateCommand _addItemCommand;
        public ICommand AddItemCommand
        {
            get
            {
                if (_addItemCommand == null)
                {
                    _addItemCommand = new DelegateCommand(AddToCart);
                }
                return _addItemCommand;
            }
        }
       public void AddToCart() {

       CartData.Add(new CartData 
         { 
            Cakename = "Black Forest", Cakeprice = 104 });

                   }
Posted
Comments
Tomas Takac 4-Mar-14 16:45pm    
It seems that you view model gets discarded after you leave the page and recreated when you navigate back to it. Show us your code around navigation and viewmodel management.
hack.luminence 5-Mar-14 0:45am    
here is my xaml code

.....

<Page.DataContext>
<vm:cartingdatasource>
</Page.DataContext>
....
<ListView
x:Name="itemListView"
AutomationProperties.AutomationId="ItemsListView"
AutomationProperties.Name="Items"
TabIndex="1"
Margin="-10,130,0,264"
Padding="120,0,0,60"

ItemsSource="{Binding cartData}"
IsSwipeEnabled="False" Grid.RowSpan="2" ItemClick="itemListView_ItemClick" SelectionChanged="itemListView_SelectionChanged_1" IsItemClickEnabled="True">
<ListView.ItemTemplate>
<datatemplate>
<grid margin="6">
<grid.columndefinitions>
<columndefinition width="Auto">
<columndefinition width="*">

<border removed="{ThemeResource ListViewItemPlaceholderBackgroundThemeBrush}" width="60" height="60">
<Image Stretch="UniformToFill" AutomationProperties.Name="{Binding Title}" Source="Assets/wewewew.jpg"/>

<stackpanel grid.column="1" margin="10,0,0,0">
<textblock text="{Binding Cakename}" style="{StaticResource TitleTextBlockStyle}" textwrapping="NoWrap" maxheight="40">
<textblock text="{Binding Cakeprice}" style="{StaticResource CaptionTextBlockStyle}" textwrapping="NoWrap">



</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="FrameworkElement">
<setter property="Margin" value="0,0,0,10">
</Style>
</ListView.ItemContainerStyle>
</ListView>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900