Reputation: 715
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");
}
}
Upvotes: 0
Views: 2518
Reputation: 229
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}" />
Upvotes: 1
Reputation: 4774
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.
Upvotes: 0
Reputation: 69959
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
.
Upvotes: 0