Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8970593
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:44:53+00:00 2026-06-15T17:44:53+00:00

I have a situation in a new WinRT app where I need to manage

  • 0

I have a situation in a new WinRT app where I need to manage an ID property on a collection of objects. Essentially I’m holding the unique ID for each object which I need to increment for each new object added. This is because I’ll be serializing to XML to save the data so need to manage this ID myself. If I was using SQL it would be an auto incrementing field.

The best way I could come up with was to set this using a method called from the constructor and then have a collection changed handler help me to update the value each time.

Here is the view model class:

using MM.Models;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Linq;

namespace MM.ViewModels
{
    public class VehiclesViewModel : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged = delegate { };

        public VehiclesViewModel()
        {
            Vehicles = new ObservableCollection<Vehicle>();
            NewVehicle = new Vehicle();
            NextVehicleID = CalculateHighestID(Vehicles.AsQueryable()) + 1;
            Vehicles.CollectionChanged += new NotifyCollectionChangedEventHandler(VehicleCollectionChanged);
        }

        private ObservableCollection<Vehicle> _vehicles;
        public ObservableCollection<Vehicle> Vehicles
        {
            get 
            { 
                return _vehicles; 
            }
            set
            {
                if (_vehicles != value)
                {
                    _vehicles = value;
                    PropertyChanged(this, new PropertyChangedEventArgs("Vehicles"));
                }
            }
        }

        void VehicleCollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
        {
            if (e.Action == NotifyCollectionChangedAction.Add)
            {
                NextVehicleID += 1;
            }
        }

        public Vehicle NewVehicle { get; set; }

        private int _nextVehicleID;
        public int NextVehicleID 
        { 
            get 
            { 
                return _nextVehicleID; 
            }
            private set
            {
                _nextVehicleID = value;
                PropertyChanged(this, new PropertyChangedEventArgs("NextVehicleID"));
            }
        }

        private int CalculateHighestID(IQueryable<Vehicle> vehicles)
        {
            var query = vehicles.OrderByDescending(v => v.VehicleID).FirstOrDefault();
            if (query != null)
            {
                return query.VehicleID;
            }
            else
            {
                return 1;
            }
        }

    }
}

and here is a text button click method I added on the xaml page to add an item.

private void Button_Click_1(object sender, Windows.UI.Xaml.RoutedEventArgs e)
{
    vm.Vehicles.Add(new Vehicle { VehicleID = vm.NextVehicleID });
}

However, the VehicleCollectionChanged is never called. As a test I used the same code to add a vehicle from the constructor method and that did work.

Can anyone explain why the method would not be called with adding a vehicle from the xaml button click?

Also, is there a better overall approach to keep track of an ID value for the next record?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:44:54+00:00Added an answer on June 15, 2026 at 5:44 pm

    Does your ID have to be an int (or incremental for that matter)? Could it be a GUID? At least then you could leave the creation up to the Vehicle class. As for why the event isn’t being called, are you ever re-assigning the “Vehicles” property on your viewmodel? Is there a reason you have a public “set” for that property? You could potentially set a new ObservableCollection to “Vehicles” and not 1) unhook from the old event and 2) hook-up the CollectionChanged event to the new collection.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a situation, where I need to create a new JavaScript object that
I have situation where I need to change the order of the columns/adding new
I have a situation where I need to access an object that has been
I am new to Python (2.6), and have a situation where I need to
I have next situation: Connection manager should have each time one object of ConnectionServer
I have a situation where I need to edit an enumerable property in a
I'm new to NHibernate and Fluent NHibernate. Assuming I have a situation like the
I'm new to Java and have gotten myself into a situation where it's evident
I am new to JavaScript so please be understanding of my situation. I have
My situation is simple. I have this in my program: File folder = new

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.