Hi all,
Today i tried joining a date and time picker controls in a new DateTimePicker control. It works fine from the codebehind but our project is following the MVVM model so i need to bind this control with XAML to our context variable.
The code for the control is (sorry its the full code but i dont know where the problem is):
public partial class DateTimePicker : UserControl, INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public static readonly DependencyProperty ValueProperty =
DependencyProperty.Register("DateTimeValue2", typeof(DateTime), typeof(DateTimePicker), new PropertyMetadata(ValueChanged));
private DatePicker datePicker = new DatePicker();
private TimePicker timePicker = new TimePicker();
private static void ValueChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
DateTimePicker r = (DateTimePicker)d;
if (e.NewValue == null)
{
r.DateTimeValue = DateTime.Now;
}
else
r.DateTimeValue = (DateTime)e.NewValue;
}
public DateTimePicker()
{
InitializeComponent();
stackPanel.Children.Add(datePicker);
stackPanel.Children.Add(timePicker);
datePicker.SelectedDateChanged +=new EventHandler<SelectionChangedEventArgs>(OnSelectionChanged);
}
private void OnSelectionChanged(Object source, SelectionChangedEventArgs e)
{
DateTimeValue = (DateTime) datePicker.SelectedDate;
}
public DateTime DateTimeValue2
{
get;
set;
}
public DateTime DateTimeValue
{
set
{
datePicker.SelectedDate = value;
timePicker.Value = value;
DateTimeValue2 = value;
RaisePropertyChanged("DateTimeValue2");
}
get
{
DateTime date = (DateTime)datePicker.SelectedDate;
DateTime time = (DateTime)timePicker.Value;
string sDate = date.ToShortDateString();
string sTime = time.ToString();
string s1 = date.ToString("yyyy-MM-ddTHH\\:mm\\:ssZ");
string s2 = date.ToString("yyyy-MM-dd");
string s3 = time.ToString("HH\\:mm\\:ss");
return DateTime.Parse(s2 + "T" + s3 + "Z");
}
}
private void RaisePropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
The XAML part where the binding is done is:
<DataTemplate>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition MinWidth="100" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<infor:Label Content="Initial Value" VerticalAlignment="Center" Grid.Column="0" Margin="3,3,0,0" Width="100"/>
<components:DateTimePicker DateTimeValue2="{Binding Path=Variable.initialValue, Mode=TwoWay, Converter={StaticResource DateTimeConverter}}" Grid.Column="1" Margin="3,3,0,0"/>
</Grid>
</DataTemplate>
The DateTimeValue2 variable i added just to test the binding to a variable that hold the DatTime value just in case SL skips the get method when fetching the data. I tried binding to DateTimeValue as well and it had no results.
At this point the Converter is never called besides the first loading of this XAML. No changes on the datepicker triger the converter and the data on Variable.initialValue is never set.
Any pointer to what i am doing wrong would be more than welcome.