The following forum(s) have migrated to Microsoft Q&A (Preview): Developing Universal Windows apps!
Visit Microsoft Q&A (Preview) to post new questions.

Learn More

 locked
Windows Phone Toolkit TimePicker Source and DataBinding Property Update RRS feed

  • Question

  • Hello,

    I want to work with a DateTime Property:

    private DateTime _timeOver;
    public DateTime TimeOver
        {
            get { return _timeOver; }
            set
            {
                _timeOver = value;
                OnPropertyChanged();
            }
        }

    I set a two-way binding to Windows Phone Toolkit's TimePicker:

    <tk:TimePicker x:Name="TimeOver"
    Value="{Binding TimeOver, Mode=TwoWay}"/>

    Unfortunately the binding works only in one direction Property->TimePicker and not the opposite. I had the same Problem with Textboxes before but solved it using this:

    public void TextBoxHandler(object sender, TextChangedEventArgs e)
        {
            TextBox txtbox = sender as TextBox;
            BindingExpression bindingExpression =
            txtbox.GetBindingExpression(TextBox.TextProperty);
            bindingExpression.UpdateSource();
        }

    I wasn't able to build something similar so far...

    Does anyone have a solution?

    Thanks...

    Tuesday, May 6, 2014 12:46 PM

Answers

  • Problem solved by this:

    bool TimePickerReturn=false;
    
     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (!TimePickerReturn)
            _positionViewModel.DataLoad();
        }
    
    private void TimeOver_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            TimePicker time = sender as TimePicker;
            BindingExpression bindingExpression = 
            time.GetBindingExpression(
            TimePicker.ValueProperty);
            bindingExpression.UpdateSource();
        }
    
    private void TimeOver_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            TimePickerReturn = true;
        }

    The idea behind is that the data are loaded only when nothing has changed by the user. Putting the TimePickerReturn into the ValueChanged Eventhandler would always prevent the page from loading data, because loading the page fires the ValueChanged Event, because it changes value by loading data from the ViewModel which has stored the latest value into isolated storage. Now I am using the tap Event to check whether the value has changed by a human.

    If anyone has a more elegant solution, I would appreciate it a lot.

    • Marked as answer by Marcus Runge Tuesday, May 6, 2014 3:32 PM
    Tuesday, May 6, 2014 2:48 PM

All replies

  • There is an eventhandler

    protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            _positionViewModel.DataLoad();
        }
    loading data from the ViewModel to the page. I figured out that the TimePicker takes the "old" value from "TimeOver" when returning from the picker page before "TimeOver" gets updated by user input. How do I manage to update "TimeOver" before "OnNavigatedTo" loads data?

    Tuesday, May 6, 2014 2:09 PM
  • Problem solved by this:

    bool TimePickerReturn=false;
    
     protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e)
        {
            base.OnNavigatedTo(e);
            if (!TimePickerReturn)
            _positionViewModel.DataLoad();
        }
    
    private void TimeOver_ValueChanged(object sender, DateTimeValueChangedEventArgs e)
        {
            TimePicker time = sender as TimePicker;
            BindingExpression bindingExpression = 
            time.GetBindingExpression(
            TimePicker.ValueProperty);
            bindingExpression.UpdateSource();
        }
    
    private void TimeOver_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
            TimePickerReturn = true;
        }

    The idea behind is that the data are loaded only when nothing has changed by the user. Putting the TimePickerReturn into the ValueChanged Eventhandler would always prevent the page from loading data, because loading the page fires the ValueChanged Event, because it changes value by loading data from the ViewModel which has stored the latest value into isolated storage. Now I am using the tap Event to check whether the value has changed by a human.

    If anyone has a more elegant solution, I would appreciate it a lot.

    • Marked as answer by Marcus Runge Tuesday, May 6, 2014 3:32 PM
    Tuesday, May 6, 2014 2:48 PM