I am using TimePicker from MaterialDesignInXamlToolkit. But it does not have a
SelectedTimeChanged
event similar to WPF stock DatePicker's SelectedDateChanged
event.
Currently I am binding the TimePicker's Text to get something similar. But the problem is that the Text gets updated a couple of times before user finishes picking the time.
Is anyone familiar with the toolkit and knows any work arounds?
XAML :
<UserControl x:Class="DateTimePickerTest.CustomDateTimePicker"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:local="clr-namespace:DateTimePickerTest"
xmlns:materialDesign="http://materialdesigninxaml.net/winfx/xaml/themes"
mc:Ignorable="d"
d:DesignHeight="500" d:DesignWidth="600">
<Grid>
<materialDesign:TimePicker Name="timePicker" Width="100" Height="30"
Text="{Binding RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type local:CustomDateTimePicker}},
Path=Time}">
</materialDesign:TimePicker>
</Grid>
</UserControl>
C#:
using System.Windows;
using System.Windows.Controls;
namespace DateTimePickerTest
{
public partial class CustomDateTimePicker : UserControl
{
private string _Time;
public string Time
{
get { return _Time; }
set
{
_Time = value;
TimePicked();
}
}
public CustomDateTimePicker()
{
InitializeComponent();
}
public void TimePicked()
{
if (timePicker.SelectedTime != null)
{
_Time = timePicker.SelectedTime.Value.ToString("hh:mm tt");
MessageBox.Show(_Time);
}
}
}
}
Try using the SelectedTime
property instead of Text
property of TimePicker
control. Also set UpdateSourceTrigger=LostFocus
. Like,
<mDesign:TimePicker Name="timePicker" Width="100" Height="30"
SelectedTime="{Binding Time, RelativeSource={RelativeSource Mode=FindAncestor,
AncestorType={x:Type local:CustomDateTimePicker},
UpdateSourceTrigger=LostFocus}">
</mDesign:TimePicker>