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 7895761
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T07:43:18+00:00 2026-06-03T07:43:18+00:00

I’m currently having an issue with WP7’s TimePicker, specifically with binding it to a

  • 0

I’m currently having an issue with WP7’s TimePicker, specifically with binding it to a ViewModel. The TimePicker in question sets the time of day for an alarm. When the page is first loaded, the TimePicker correctly displays the value of the Alarm object (the default value of 12:00am in this case). However, when the user chooses a new value this is not reflected in the model – it’s overridden with the previous value of 12:00am.

I’m using MVVM to create this form and hold the data bindings. Is there anything in particular that I’m doing wrong?

(View) AlarmEditorControl.xaml

<TextBlock Height="30" HorizontalAlignment="Left" Margin="1,6,0,0" Name="lblAlarmTime" Text="Alarm Time:" VerticalAlignment="Top" Grid.Column="2" FontSize="26" />
<!-- Data binding isn't working for updates! -->
<toolkit:TimePicker HorizontalAlignment="Left" Margin="140,34,0,0" Name="tpAlarmTime" VerticalAlignment="Top" Width="161" Grid.Column="1" Grid.ColumnSpan="2" Value="{Binding Path=Time, Mode=TwoWay}" />

(ViewModel) AlarmEditorModel.cs

[DataContractAttribute]
public class AlarmEditorModel
{
    private int _index;

    [DataMemberAttribute]
    public Alarm Alarm { get; set; }

    [DataMemberAttribute]
    public int Index
    {
        get
        {
            return _index;
        }

        set
        {
            _index = value;
        }
    }

    public AlarmEditorModel(int index)
    {
        _index = index;
        Alarm = new Alarm();

        // Get the list of alarms
        AlarmSerializer serializer = new AlarmSerializer();

        // Check the index is in range
        List<Alarm> alarms = serializer.AlarmList;
        if (_index > -1 && index < alarms.Count)
        {
            Alarm = alarms[_index];
        }
    }

    public void Commit()
    {
        // Get the current list of alarms
        AlarmSerializer serializer = new AlarmSerializer();
        List<Alarm> alarms = serializer.AlarmList;

        // Replace our new value
        alarms[_index] = Alarm;
        serializer.AlarmList = alarms;
    }
}

(Model) Alarm.cs

[DataContract]
public class Alarm : INotifyPropertyChanged
{
    private bool _active;
    private DateTime _time;

    [DataMember]
    public string Name { get; set; }

    [DataMember]
    public DateTime Time
    {
        get
        {
            return _time;
        }
        set
        {
            if (_time != value)
            {
                _time = value;
                RaisePropertyChanged("Time");
            }
        }
    }

    [DataMember]
    public AlarmFrequency Frequency { get; set; }

    [DataMember]
    public AlarmTone Tone { get; set; }

    [DataMember]
    public bool Active { 
        get {
            return _active;
        }
        set {
            _active = value;
        } 
    }

    public string AlarmTimeString { 
        get {
            return Time.ToShortTimeString();
        }
    }



    /**
     * Default Constructor
     */
    public Alarm()
    {
        Debug.WriteLine("Alarm: Using default constructor");
        this.Name = "New Alarm";
        this.Time = DateTime.Today;
        this.Frequency = new AlarmFrequency();
        this.Tone = new AlarmTone();
        this.Active = true;

        Debug.WriteLine("Alarm hours is " + this.Time.Hour);
    }

    /**
     * Parameterised constructor
     */
    public Alarm(string Name, DateTime Time, AlarmFrequency Frequency,
                    AlarmTone Tone, bool Active)
    {
        Debug.WriteLine("Alarm: Using parameterised constructor");
        this.Name = Name;
        this.Time = Time;
        this.Frequency = Frequency;
        this.Tone = Tone;
        this.Active = Active;
       }
}

(Calling Page) NewAlarm.xaml.cs

 private List<Channel> feeds;
    private AlarmEditorModel _aem;
    private int _index;

    public NewAlarm()
    {
        InitializeComponent();

        feeds = new List<Channel>();
        feeds.Add(new Channel(null, null, "Feed 1", DateTime.Now));
        feeds.Add(new Channel(null, null, "Feed 2", DateTime.Now));
    }

    /**
     * Setup functions when the page is loaded
     */
    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
        // Function vars + debug

        Debug.WriteLine("Navigating to");

        // Check if we're recovering from tombstone
        if (!StateUtilities.IsLaunching && this.State.ContainsKey("AlarmState"))
        {
            // Recover the saved model
            _aem = (AlarmEditorModel)this.State["AlarmState"];
        }
        else 
        {
            try
            {
                // Editing an alarm.
                _index = Convert.ToInt32(this.NavigationContext.QueryString["index"]);
                Debug.WriteLine("Editing an alarm");
            }
            catch (KeyNotFoundException knfe)
            {
                Debug.WriteLine(knfe.Message);

                // No index provided, new alarm
                _index = -1;
            }

            // Set the model from the index
            _aem = new AlarmEditorModel(_index);
        }

        AlarmEditor.DataContext = _aem.Alarm;
        Debug.WriteLine(_aem.Alarm.Time.Hour);
    }

    /**
     * Preserve alarm details when tombstoning
     */
    protected override void OnNavigatedFrom(System.Windows.Navigation.NavigationEventArgs e)
    {
        base.OnNavigatedFrom(e);

        if (this.State.ContainsKey("AlarmState"))
        {
            this.State["AlarmState"] = _aem;
        }
        else
        {
            this.State.Add("AlarmState", _aem);
        }

        StateUtilities.IsLaunching = false;
    }

EDIT 1

It would appear the setter for Alarm.Time is being called twice. By adding the following debug lines to the Time property:

[DataMember]
    public DateTime Time
    {
        get
        {
            return _time;
        }
        set
        {
            Debug.WriteLine("Current time is " + _time.ToShortTimeString());
            Debug.WriteLine("New time is " + value.ToShortTimeString());

            if (_time != value)
            {
                Debug.WriteLine("Changing time value");
                _time = value;
                RaisePropertyChanged("Time");
            }
        }
    }

The following output is produced in the log when setting the time to 9:10am:

Current time is 4:00 AM
New time is 9:10 AM 
Changing time value
Current time is 12:00 AM 
New time is 4:00 AM 
Changing time value
  • 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-03T07:43:19+00:00Added an answer on June 3, 2026 at 7:43 am

    Problem solved I think. I needed to make an additional check in OnNavigatedTo when recovering from a Tombstone in order to get the value of the TimePicker before it was overwritten by the ViewModel:

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
    {
            // Check if we're recovering from tombstone
            if (!StateUtilities.IsLaunching && this.State.ContainsKey("AlarmState"))
            {
                // Recover the saved model
                _aem = (AlarmEditorModel)this.State["AlarmState"];
    
                // Use the value from the TimePicker
                _aem.Alarm.Time = (DateTime)AlarmEditor.tpAlarmTime.Value;
            }
            else 
            ...
    

    Need to do a few more tests on this solution, but it seems to be doing the job so far.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
Specifically, suppose I start with the string string =hello \'i am \' me And
I am currently running into a problem where an element is coming back from
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

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.