locked
How can I take action when a ListView changes? RRS feed

  • Question

  • Hi,
    I have a ListView and I want to detect when
    it changes and fill data in a TextBlock with
    info about new situation.

    Thanks


    ADRIAN DIBU

    Monday, January 6, 2014 2:29 AM

Answers

  • Hi,

    Problem solved

    public FoodNew()
            {
                this.InitializeComponent();
                this.navigationHelper = new NavigationHelper(this);
                this.navigationHelper.LoadState += navigationHelper_LoadState;
                this.navigationHelper.SaveState += navigationHelper_SaveState;
             
    // Register here or in RegisterEventHandler(), choose one way you like. Both are working

                this.FoodDListView.SelectionChanged += new SelectionChangedEventHandler(foodDListView_SelectionChanged);
                this.FoodDListView.SizeChanged += new SizeChangedEventHandler(foodDListView_SizeChanged);

            }

            private void RegisterEventHandler()
            {
                this.FoodDListView.SizeChanged += new SizeChangedEventHandler(foodDListView_SizeChanged);
               this.FoodDListView.SelectionChanged += new SelectionChangedEventHandler(foodDListView_SelectionChanged);

            }

           private void foodDListView_SizeChanged(object sender, SizeChangedEventArgs e)
        
        {
               txtTotalFood.Text = App.pFood;
           }

       
            private void foodDListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
              
                txtTotalFood.Text = App.pFood;

            }

    Thanks


    ADRIAN DIBU

    • Marked as answer by adibu Tuesday, January 14, 2014 3:06 AM
    Tuesday, January 14, 2014 3:06 AM

All replies

  • You'll need to explain exactly how the ListView changes.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.

    Monday, January 6, 2014 2:40 PM
    Moderator
  • Hi,
    Thanks for the prompt answer.
    I am using command to delete items
          public FoodDViewModel()
            {
                // create a DeleteCommand instance
                this.DeleteCommand = new DelegateCommand(ExecuteDeleteCommand);
                // Get data source
               FoodDs = InitializeSampleDataS.GetData();
            }
            #endregion

            #region Execute and CanExecute methods
            void ExecuteDeleteCommand(object param)
            {
                int cal = 0;
                int calApp = 0;
                int calTot = 0;
                int id = (Int32)param;
                  FoodD cus = GetFoodDById(id);
                   if (cus != null)
                   {
                    
                       //Remove Item
                     FoodDs.Remove(cus);
                  // Calculate new total
                     cal = System.Convert.ToInt32(cus.calories);
                     calApp = System.Convert.ToInt32(App.pFood);
                     calTot = calApp - cal;
                    
                     App.pFood = calTot.ToString();
                     App.myValueFood = App.pFood;
                     roamingSettings.Values["MyValueFood"] = App.myValueFood;
                       //  Delete record by id
                     FoodMViewModel foodMViewModel = new FoodMViewModel();
                     string result = foodMViewModel.DeleteFoodM(id);
                 //    System.Diagnostics.Debug.WriteLine(" Delete FoodM result: " + result);
              
                 }
            }
    // Get the deleting item by Id property
            private FoodD GetFoodDById(int id)
            {
                  return FoodDs.First(x => x._id== id);
            }

    To see after delete
       foodDViewModel = new FoodDViewModel();

       FoodDListView.DataContext = foodDViewModel; 

    Now I need to detect the nr of items changed and update the total which I know and it is App.pFood

    Thanks


    ADRIAN DIBU

    Monday, January 6, 2014 11:25 PM
  • I'm hoping an MVVM expert will chime in here, I'm not it.

    Matt Small - Microsoft Escalation Engineer - Forum Moderator
    If my reply answers your question, please mark this post as answered.

    NOTE: If I ask for code, please provide something that I can drop directly into a project and run (including XAML), or an actual application project. I'm trying to help a lot of people, so I don't have time to figure out weird snippets with undefined objects and unknown namespaces.


    Thursday, January 9, 2014 4:34 PM
    Moderator
  • Hi,
    Thanks Matt Small for the prompt answer.
    My problem is not MVVM is related to the fact
    I don't know how to detect when a ListView
    changes the content and take some action.
    It exist for ViewList:
    DataContextChanged,
    ContainerContentChanging,
    SelectionChanged,
    but I don't know how to use it.

    Thanks
    Best regards


    ADRIAN DIBU

    Friday, January 10, 2014 11:13 PM
  • Hi

    You can use a winrtbehaviors to bind the command to viewmodel.

    specifically EventToCommandBehavior -SelectionChanged event will work

    <interactivity:Interaction.Behaviors>
      <Core:EventTriggerBehavior EventName="SelectionChanged">
        <Core:InvokeCommandAction Command="{Binding StartGame}"/>
    </Core:EventTriggerBehavior>

    <Core:EventTriggerBehavior EventName="Unloaded"> <Core:InvokeCommandAction Command="{Binding SuspendGame}"/> </Core:EventTriggerBehavior> </interactivity:Interaction.Behaviors>


    http://dotnetbyexample.blogspot.nl/2013/10/introducing-win8nl-for-windows.html

    Hope this helps

    Regards



    Saturday, January 11, 2014 6:18 AM
  • HI

    You can hook Selectionchanged /Tapped event based on your needs.

    Regards

    Saturday, January 11, 2014 6:37 AM
  • Hi,

    For the answers.

    I execute the delete command from the button Delete part of ListView

     <StackPanel Orientation="Vertical" Margin="370,0,0,0">
             <Button Content="Delete" Margin="0,0,0,0"
                    Command="{Binding DataContext.DeleteCommand, ElementName=FoodDListView}"
                                                CommandParameter="{Binding _id}"                                      
                                                />
     </StackPanel>

     #region Execute and CanExecute methods
            void ExecuteDeleteCommand(object param)
            {
                int cal = 0;
                int calApp = 0;
                int calTot = 0;

                int id = (Int32)param;
                  FoodD cus = GetFoodDById(id);
                   if (cus != null)
                   {
                       //Remove Item
                     FoodDs.Remove(cus);

                  // Calculate new total
                     cal = System.Convert.ToInt32(cus.calories);
                     calApp = System.Convert.ToInt32(App.pFood);
                     calTot = calApp - cal;
                    
                     App.pFood = calTot.ToString();
                     App.myValueFood = App.pFood;
                     roamingSettings.Values["MyValueFood"] = App.myValueFood;

                     //  Delete record by id
                     FoodMViewModel foodMViewModel = new FoodMViewModel();
                     string result = foodMViewModel.DeleteFoodM(id);
                 }
            }

            #endregion

         After Deleting the item of the ViewList
     DataContextChanged,
     ContainerContentChanging,
     SelectionChanged, happen
    and I haven't any other interaction like tap , click etc
    so I suppose to detect the change in ViewList and
    modify the TextBlock txtTotal which is part of XAML not of ViewList

    Thanks


    ADRIAN DIBU

    Saturday, January 11, 2014 5:51 PM
  • Hi

    Correct me if am wrong,

    As far as i understood. You have a list view and a delete button, when you press delete the list itemsource is updated and and is properly bind to UI. You have a Textblock which is a total of some amount, but your textblock doesn;t update. how to do this?

    In this scenario you will have to sum up your total from execute method and update the UI by setting the textblock binded property.

    Let us know if you have more doubts...

    Regards

    Varun

    Saturday, January 11, 2014 7:34 PM
  • Hi,

    Thanks for answer.

     

    When I hit Delete I have the total see:  

                     // Calculate new total
                     cal = System.Convert.ToInt32(cus.calories);
                     calApp = System.Convert.ToInt32(App.pFood);
                     calTot = calApp - cal;
                    
                     App.pFood = calTot.ToString();
                     App.myValueFood = App.pFood;
                     roamingSettings.Values["MyValueFood"] = App.myValueFood;

     

    Now how can I update the txtTotal TextBlock it is not binded.?

    txtTotal belong to XAML not to ListView to be able to make binding.

    Thaks

     


    ADRIAN DIBU

    Saturday, January 11, 2014 10:05 PM
  • Hi,
    I added:  

      private void foodDListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
                txtTotalFood.Text = App.pFood;
             }

          protected override void OnNavigatedTo(NavigationEventArgs e)
            {
                FoodDListView.SelectionChanged += new SelectionChangedEventHandler(foodDListView_SelectionChanged);

               navigationHelper.OnNavigatedTo(e);
            }
         and in XAML
      <ListView x:Name="FoodDListView"
                              Visibility="Visible"
                              AutomationProperties.AutomationId="FoodDListView"
                              AutomationProperties.Name="FoodD Group"    
                              ScrollViewer.VerticalScrollBarVisibility="Visible"
                              ScrollViewer.VerticalScrollMode="Enabled"
                              BorderThickness="1"
                              BorderBrush="Yellow"
                              Background="Navy"
                              ItemsSource="{Binding FoodDs}"
                              SelectionChanged="foodDListView_SelectionChanged"
                        >
    Now when I select an item txtTotalFood is updated.

    How can I fire the event after Delete command executes not when
    I select an item by hand?
    Selection is changed after removing an item.
    Thanks


    ADRIAN DIBU

    Monday, January 13, 2014 11:04 PM
  • Hi,

    Problem solved

    public FoodNew()
            {
                this.InitializeComponent();
                this.navigationHelper = new NavigationHelper(this);
                this.navigationHelper.LoadState += navigationHelper_LoadState;
                this.navigationHelper.SaveState += navigationHelper_SaveState;
             
    // Register here or in RegisterEventHandler(), choose one way you like. Both are working

                this.FoodDListView.SelectionChanged += new SelectionChangedEventHandler(foodDListView_SelectionChanged);
                this.FoodDListView.SizeChanged += new SizeChangedEventHandler(foodDListView_SizeChanged);

            }

            private void RegisterEventHandler()
            {
                this.FoodDListView.SizeChanged += new SizeChangedEventHandler(foodDListView_SizeChanged);
               this.FoodDListView.SelectionChanged += new SelectionChangedEventHandler(foodDListView_SelectionChanged);

            }

           private void foodDListView_SizeChanged(object sender, SizeChangedEventArgs e)
        
        {
               txtTotalFood.Text = App.pFood;
           }

       
            private void foodDListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
            {
              
                txtTotalFood.Text = App.pFood;

            }

    Thanks


    ADRIAN DIBU

    • Marked as answer by adibu Tuesday, January 14, 2014 3:06 AM
    Tuesday, January 14, 2014 3:06 AM