views:

20

answers:

1

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;
                }
            }
        }
    }
}
+1  A: 

I can see nothing wrong with the code you have posted so far. There is an assumption that OnPropertyChanged is firing the PropertyChanged event of INotifyPropertyChanged implemented by the containing class.

My attention would be on the Binding itself. These usually fail silently, if the property path is not resolvable nothing usually happens.

Are you sure you need to reference the LayoutRoot by name? Does the control not already sit inside LayoutRoot without other ancestor controls having their DataContext diverted elsewhere? Why is two way binding needed, that seems highly odd? Try simply:-

<views:NotificationView x:Name="NotificationView" Notification="{Binding Notification}" ... />  
AnthonyWJones
Trust me I have done that also, it is just left over code that I tried to use instead of the one you just mentioned, but works the same though. Yes it must be somehow silently failing and I just can't pin point how or where.
Mike Flynn