public AddOrEditFillupEntry()
 {
     InitializeComponent();
     _isNewInstance = true;
     if (DesignerProperties.IsInDesignTool)
     {
         DataContext = new AddOrEditFillupEntryViewModel
                           {
                               FillupEntry =
                                   new FillupEntry
                                       {
                                           EntryDate = DateTime.Now,
                                           PumpUnit = UnitOfMeasure.DefaultVolumeUnit
                                       }
                           };
     }
 }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);

            if (!_isNewInstance) return;

            string mode;
            string vehicleIdString;
            NavigationContext.QueryString.TryGetValue("mode", out mode);
            NavigationContext.QueryString.TryGetValue("vehicleId", out vehicleIdString);
            int vehicleId = int.Parse(vehicleIdString);
            var entryMode = (Mode) (Enum.Parse(typeof (Mode), mode, true));
            var viewModel = new AddOrEditFillupEntryViewModel {Vehicle = App.VehicleStore[vehicleId], Mode = entryMode};

            switch (entryMode)
            {
                case Mode.New:
                    viewModel.FillupEntry = new FillupEntry
                                                {
                                                    EntryDate = DateTime.Now,
                                                    PumpUnit = UnitOfMeasure.DefaultVolumeUnit,
                                                    OdometerUnit = viewModel.Vehicle.OdometerUnit,
                                                };
                    break;

                case Mode.Edit:
                    string entryTicksString;
                    NavigationContext.QueryString.TryGetValue("entryTicks", out entryTicksString);
                    long entryTicks = long.Parse(entryTicksString);
                    viewModel.OriginalEntryTicks = entryTicks;
                    _originalEntry = viewModel.Vehicle.Entries
                        .Where(entry => entry.EntryDate.Ticks == entryTicks)
                        .Single();

                    viewModel.FillupEntry = FillupEntry.Clone(_originalEntry);
                    break;

                case Mode.Insert:
                    string fromDateString;
                    string toDateString;
                    NavigationContext.QueryString.TryGetValue("fromDate", out fromDateString);
                    NavigationContext.QueryString.TryGetValue("toDate", out toDateString);
                    var fromDate = new DateTime(long.Parse(fromDateString));
                    var toDate = new DateTime(long.Parse(toDateString));
                    viewModel.InsertDateFrom = fromDate;
                    viewModel.InsertDateTo = toDate;

                    viewModel.FillupEntry = new FillupEntry
                                                {
                                                    PumpUnit = UnitOfMeasure.DefaultVolumeUnit,
                                                    OdometerUnit = viewModel.Vehicle.OdometerUnit,
                                                    EntryDate = DateTime.Today
                                                };
                    break;
                default:
                    throw new Exception("We should never get here.");
            }

            DataContext = viewModel;

            //restore previous state
            if (!State.ContainsKey("StatePreserved")) return;
            StateUtils.RestoreState(State, TxtFillAmount, String.Empty);
            StateUtils.RestoreState(State, TxtOdometerReading, String.Empty);
            StateUtils.RestoreState(State, TxtPricePerUnit, String.Empty);
            StateUtils.RestoreState(State, EntryDatePicker, DateTime.Now);
            StateUtils.RestoreState(State, chkNotToppedOff, false);
            StateUtils.RestoreState(State, chkMissedPreviousEntry, false);
        }