0

In my WPF application, I've used TimePicker control from extended WPF Toolkit. When I run application, TimePicker control should display default value as "12:00 AM". Currently it's showing me blank. How to achieve this result?

Xaml code:

<xctk:TimePicker x:Name="StartValue" Value="{Binding StartTimeBinding, 
    ElementName=MainWin, Mode=TwoWay}" Format="Custom" FormatString="hh:mm tt" 
    Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="100" 
    EndTime="11:59:0"/>
<xctk:TimePicker x:Name="StopValue" Value="{Binding StopTimeBinding, 
     ElementName=MainWin, Mode=TwoWay}" Format="Custom" FormatString="hh:mm tt" 
    Background="Yellow" Padding="0" Margin="0" BorderThickness="0" Width="60" 
    EndTime="11:59:0"/>

TimePicker controls bound to below properties:

public string StartTimeBinding 
{ 
    set
    { 
        this._id = value;
    } 
    get
    { 
        return this._started_at.ToString("h:mm tt");
    } 
}

public string StopTimeBinding 
{ 
    set
    {
        this._id = value;
    } 
    get
    { 
        return this._ended_at.ToString("h:mm tt");
    } 
}
user2622971
  • 685
  • 3
  • 10
  • 20
  • 1
    I assume you implemented INotifyPropertyChanged interface in your ViewModel class, Did you ? – Nitesh Aug 01 '13 at 07:32
  • Yes. But what is the relation of INotifyPropertyChanged interface interface with this? Can you explain? – user2622971 Aug 01 '13 at 07:37
  • 1
    @Nitesh Even if the OP did, he isn't calling OnPropertyChanged when he sets the value, so it doesn't matter. – JMK Aug 01 '13 at 08:30

3 Answers3

1

set this property in your view model:

 private string _StartShift;
    public string StartShift
    {
        get { return _StartShift; }
        set
        {
            if (_StartShift != value)
            {
                _StartShift = value;
                OnPropertyChanged("StartShift");
            }
        }
    }

set this property in the contructor to your desired default value:

 StartShift = "6:30";

xaml code:

  <xctk:TimePicker Grid.Row="5"  Grid.Column="1" Width="auto" StartTime="5:00" Value="{Binding StartShift}" />
Bianca Kalman
  • 229
  • 4
  • 3
0

Isn`t it because TimePicker.Value is of type DateTime and you bind it to StartTimeBinding property which is of type string? Just get the value, TimePicker will do the formatting:

public DateTime StartTimeBinding 
{ 
    set
    { 
        this._id = value;
    } 
    get
    { 
        return this._started_at;
    } 
}

Edit: It appears that my advice above does not resolve the issue as TimePicker works alright with strings. So my guess it that problem lies in this part: ElementName=MainWin. There`s no MainWin element in this context, so the binding can`t find required property. It`s hard to say without seeing whole layout but maybe you can fix it with RelativeSource.

Community
  • 1
  • 1
icebat
  • 4,696
  • 4
  • 22
  • 36
0

Try removing the ToString(h:mm tt") from your properties and change the type to DateTime. The TimePicker control binds to a DateTime, not a string.

Sheridan
  • 68,826
  • 24
  • 143
  • 183