Category Archives: C# .NET

Exception: ItemsControl is inconsistent with its items source

This can get tricky if you don’t realise where its arising from.

I had one listbox with its ItemsSource Binding set to List<T> in viewmodel. Via a command, i was updating the listbox items source. Now it was fine until I added a new item to the list and fired RaisePropertyChanged() somewhere in the middle of the code for Command execution.

What happens now is that UI is asked to refresh its state before Command execution is complete. i.e. UI finds its ItemSource collection different to one on the source(ViewModel property) because the Source property on ViewModel is now increased with one additional item but UI still is hanging around with one less item in list!

The best way to resolve this was to use ObservableCollection<> so there won’t be any need for change notification. However, in principle, this error can be avoided if UI is asked refresh only after the command execution(or whatever process is updating the source collection) is done. This requires use of dispatcher.

_dispatcherService.Dispatch(() => RaisePropertyChanged(nameof(Employees)))

I have injected _dispatcherService in my class so just calling it but in essence this will defer the UI refresh until its safe to do so i.e. after current processing is over



Get current time of any country

//To find out the current time in any country, factoring in Daylight saving, we can use the following code

/* We generate all the timezones so we can pick as and when necessary */
public static readonly ReadOnlyCollection<TimeZoneInfo> TimeZones = TimeZoneInfo.GetSystemTimeZones();

/* Get the timezone of the exchange currently in operation */
TimeZoneInfo info = TimeZoneInfo.FindSystemTimeZoneById(“Indian Standard Time”);

/* Get the offset */
var dto = DateTimeOffset.UtcNow;

/*Now get the current time for the exchange */
var currentTime = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dto, info.Id);

Handy DateTimeConverter

You can use this DateTimeConverter to bind all your string values to DateTime controls and vice-versa

 public class DateTimeConverter : IConverter<string, DateTime>
            private static readonly IFormatProvider FormatProvider = 
            (IFormatProvider) CultureInfo.InvariantCulture;

     static DateTimeConverter()

     public string Convert(DateTime dateTime)
         return dateTime.ToString(DateTimeConverter.FormatProvider);

     public DateTime ConvertBack(string dateTimeString)
         return DateTime.Parse(dateTimeString, DateTimeConverter.FormatProvider);