Gert Hermans
Gert Hermans

Reputation: 829

Timepicker Updatesourcetrigger=propertychanged doesn't change value

I'm hosting a WPF usercontrol in a windows form In the wpf user control I am using a timepicker from wpfToolkit.extended

If I use the up or downkeys or just enter a time in the textfield the source is not updated allthough I am using Updatesourcetrigger = propertychanged.

When I select a time in the dropdrownlist everything works the way it should. This is the namespace of the toolkit.

xmlns:xctk="clr-namespace:Xceed.Wpf.Toolkit;assembly=WPFToolkit.Extended"

This is the xaml for the timepicker

  <xctk:TimePicker  Format="LongTime" TimeInterval="00:15:00.000" Value="{Binding Path=StartTime, UpdateSourceTrigger=PropertyChanged}" ></xctk:TimePicker>

If I click outside the WPFusercontrol without changing the focus to another control in the wpf usercontrol first. The Binded time is not updated.

Any idea how I can fix this?

Upvotes: 4

Views: 2568

Answers (3)

RonnyR
RonnyR

Reputation: 230

The Focus solution has the problem that it immediately changes the cursor, marking the hours, which prevents typing in minutes greater than 9.

To prevent this you can use a timer and setting the value directly instead of using the focus hack.

private System.Timers.Timer _delayedDateChangeTimer;
private DateTime _tmpNewDate;

public CtorView()
{
    InitializeComponent();

    _delayedDateChangeTimer = new System.Timers.Timer();
    _delayedDateChangeTimer.Elapsed += DelayedDateChangeElapsed;
    _delayedDateChangeTimer.Interval = 700;
}

private void DelayedDateChangeElapsed(object sender, ElapsedEventArgs e)
{
    App.Current.Dispatcher.BeginInvoke(new Action(() => { tpTargetTime.Value = _tmpNewDate; }));
    _delayedDateChangeTimer.Stop();
}

private void tpTargetTime_TextChanged(object sender, TextChangedEventArgs e)
{
    if (DateTime.TryParse(tpTargetTime.Text, out _tmpNewDate) && tpTargetTime.Value != _tmpNewDate)
        _delayedDateChangeTimer.Start();
}

Upvotes: 1

Gert Hermans
Gert Hermans

Reputation: 829

Found a solution for this problem: I've given the TimePicker a name (In this case 'tpFrom') then I've used the TextBoxBase.TextChanged event on the TimePicker. This is what the Xaml looks like now:

<xctk:TimePicker Name="tpFrom"  Format="LongTime"  TextBoxBase.TextChanged="TimePicker_TextChanged"  TimeInterval="00:15:00.000" Value="{Binding Path=StartTime, UpdateSourceTrigger=PropertyChanged}"></xctk:TimePicker>

In the code behind in our eventhandler we'll put the focus on our timepicker.

  private void TimePicker_TextChanged(object sender, TextChangedEventArgs e)
    {
        tpFrom.Focus();            
    }

Now everytime the text changes, the value changes as well and the problem is solved :-)

Upvotes: 4

TrueEddie
TrueEddie

Reputation: 2233

Does the TimePicker have a Text property? If so, try binding to that instead.

I think this behavior might be to prevent you from binding to a bad datetime as you type. I would guess that when focus is lost it tries to set the property and does error checking. If it did this while you typed it would constantly be changing the value anytime you make a change (say delete a character).

Is there something specific you are trying to do as you type?

Upvotes: 0

Related Questions