I am having trouble updating a dependency property in Silverlight. I am setting a property and notifying it changed in my parent class. The custom control listens to this property changing through a dependency property, but it never hits it (calls the change callback). I have been playing around with it and the only time I can get it to hit is if I set a default value, but it never takes on the new value. I am setting breakpoints and seeing the values changing, and even put a textblock with the NotificationModel.Type object to it and it changes fine. Please help!
Main.xaml
<views:NotificationView x:Name="NotificationView" Grid.Column="0" Grid.Row="1" Grid.ColumnSpan="2" Notification="{Binding Path=DataContext.Notification, ElementName=LayoutRoot, Mode=TwoWay}"></views:NotificationView>
MainViewModel.cs
void part_NotificationChanged(object sender, NotificationChangedEventArgs e)
{
Notification = new NotificationModel()
{
Notifications = e.Notification.Notifications,
Type = e.Notification.Type
};
}
private NotificationModel _notification;
public NotificationModel Notification
{
get
{
return _notification;
}
set
{
_notification = value;
this.OnPropertyChanged("Notification");
}
}
NotificationView.xaml.cs
public partial class NotificationView : UserControl
{
public NotificationView()
{
InitializeComponent();
}
public NotificationModel Notification
{
get
{
return (NotificationModel)GetValue(NotificationProperty);
}
set
{
SetValue(NotificationProperty, value);
}
}
public static readonly DependencyProperty NotificationProperty =
DependencyProperty.Register("Notification", typeof(NotificationModel), typeof(NotificationView), new PropertyMetadata(new PropertyChangedCallback(OnNotificationChanged)));
private static void OnNotificationChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
NotificationView view = d as NotificationView;
if (view != null)
{
NotificationModel notification = e.NewValue as NotificationModel;
if (notification != null)
{
switch (notification.Type)
{
case NotificationType.Success:
view.LayoutRoot.Children.Add(new SuccessView() { Message = notification.Notifications.FirstOrDefault() });
break;
case NotificationType.Error:
view.LayoutRoot.Children.Add(new ErrorsView() { Errors = notification.Notifications });
break;
case NotificationType.Caution:
break;
default:
view.LayoutRoot.Children.Clear();
break;
}
}
}
}
}