Stumped : I am in the initial stages of creating a month planner control that need to carry out some UI action I The control when supplying a date from the ViewModel bound in XAML. When I run the code, the ViewModel does return the value but the PropertyChangeCallBack is not firing. I have done exactly the same thing in an public class ExtendedDataGrid : DataGrid control and that work fine but it is almost like you have to do something different with UserControls. Here is the basis of my code:
public partial class DiaryMonthViewUserControl : UserControl
{
private DateTime _topDate;
public DiaryMonthViewUserControl()
{
InitializeComponent();
}
// Property to get the date from the ViewModel for processing. This will fire and event to set the month we want to display
public static readonly DependencyProperty TheDateProperty = DependencyProperty.Register("TheDate", typeof(DateTime),
typeof(DiaryMonthViewUserControl),
new FrameworkPropertyMetadata(DateTime.Today, OnDatePropertyChanged, null));
public void SetTheCurrentDate(DateTime CurrentDate)
{
_topDate = CurrentDate;
// Like Outlook, whatever date we supply, I want the first top level displayed date for the month
if (_topDate.Day > 1)
_topDate = _topDate.AddDays(-(_topDate.Day-1)); // First day of the month
// Get to the first Monday before the 1st of the month if not on a Monday
while (_topDate.DayOfWeek != DayOfWeek.Monday)
_topDate = _topDate.AddDays(-1);
// I will set the UI here once I have solved this problem.
MakeChangesToTheUIPlease();
}
private static void OnDatePropertyChanged(DependencyObject Source, DependencyPropertyChangedEventArgs e)
{
var control = Source as DiaryMonthViewUserControl;
if (control != null)
control.SetTheCurrentDate((DateTime)e.NewValue);
}
[Bindable(true)]
public DateTime TheDate
{
get { return (DateTime)GetValue(TheDateProperty); }
set { SetValue(TheDateProperty, value); }
}
}
I have also tried using new PropertyChangedCallback(OnDatePropertyChanged) as a parameter and that still does not work.
My binding is as follows:
<my:DiaryMonthViewUserControl HorizontalAlignment="Left" Margin="12,12,0,0" x:Name="diaryMonthViewUserControl1"
VerticalAlignment="Top" Height="325" Width="476" TheDate="{Binding Path=CurrentDate}" />
When I run the code my ViewModel breaks on the getter for CurrentDate and if I remove the CurrentDate binding then it does not. The problem is that the call back is not firing and, for the life of me, I cannot fathom why.
Any help would be much appreciated, particularly links to articles that may cover this problem.
The PropertyChanged callback only fires if the immediate property is changed, that means the whole DateTime object would need to be replaced, if properties of the DateTime object are changed the event will not be raised. This is why it works for primitive types like int.
If you want to execute some method whenever any property of the DateTime changed you could implement a DateTime class which provides notifications. You can then execute the method whenever any of the properties change.